question about macro define
-
void func(UINT nOperation) { #if (nOperation == OPERATION_1) #define OPERATION(spObj, nParam) fun1(spObj, nParam) #elif (nOperation== OPERATION_2) #define OPERATION(spObj, nParam) func2(spObj, nParam) #elif (nOperation== OPERATION_3) #define OPERATION(spObj, nParam) func3(spObj, nParam) nFrameNo) #else #define OPERATION(spObj, nParam) #endif .... OPERATION(spObj, n) // whatever nOperation be, it always call fun1,why??? }
-
void func(UINT nOperation) { #if (nOperation == OPERATION_1) #define OPERATION(spObj, nParam) fun1(spObj, nParam) #elif (nOperation== OPERATION_2) #define OPERATION(spObj, nParam) func2(spObj, nParam) #elif (nOperation== OPERATION_3) #define OPERATION(spObj, nParam) func3(spObj, nParam) nFrameNo) #else #define OPERATION(spObj, nParam) #endif .... OPERATION(spObj, n) // whatever nOperation be, it always call fun1,why??? }
Macros don't work at runtime; they are processed before compilation. Try something like this instead:
void func (UINT nOperation)
{
switch (nOperation)
{
// [edited - Thanks DavidCrow] case default:
default:
break;
case OPERATION_1:
fun1(spObj, nParam);
break;
case OPERATION_2:
fun2(spObj, nParam);
break;
case OPERATION_3:
fun3(spObj, nParam);
break;
}
}-- jlr http://jlamas.blogspot.com/[^]
-
void func(UINT nOperation) { #if (nOperation == OPERATION_1) #define OPERATION(spObj, nParam) fun1(spObj, nParam) #elif (nOperation== OPERATION_2) #define OPERATION(spObj, nParam) func2(spObj, nParam) #elif (nOperation== OPERATION_3) #define OPERATION(spObj, nParam) func3(spObj, nParam) nFrameNo) #else #define OPERATION(spObj, nParam) #endif .... OPERATION(spObj, n) // whatever nOperation be, it always call fun1,why??? }
:omg: You've mixed up precompiler directives with code. Macro expansion (and execution of other precompiler directives) happen before code is compiled. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Macros don't work at runtime; they are processed before compilation. Try something like this instead:
void func (UINT nOperation)
{
switch (nOperation)
{
// [edited - Thanks DavidCrow] case default:
default:
break;
case OPERATION_1:
fun1(spObj, nParam);
break;
case OPERATION_2:
fun2(spObj, nParam);
break;
case OPERATION_3:
fun3(spObj, nParam);
break;
}
}-- jlr http://jlamas.blogspot.com/[^]
Jose Lamas Rios wrote: case default: No
case
needed here.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Jose Lamas Rios wrote: case default: No
case
needed here.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
DavidCrow wrote: Jose Lamas Rios wrote: case default: No case needed here. Yep, you are right. -- jlr http://jlamas.blogspot.com/[^]