I am currently working on an already developed project written in MFC C++ and am facing a problem with an already present macro having the definition:
#define HEIGHT_TESTS 13
I am trying to change the value from within the code but I think since its a preprocessed definition, I am unable to do that. Is there a way I could get around this problem without having to change the original macro overall (as it might affect the original functionality of the program). I am just intending to change it in one particular condition, rest everywhere else it remains the same.
Just to let everyone know, I have obviously tried out using a different macro definition with the value (17) I am intending to use, but no luck as such.
Any help would be much appreciated.
A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;).
You can't. Macros are expanded by the Preprocessor, which happens even before the code is compiled. It is a purely textual replacement. If you need to change something at runtime, just replace your macro with a real function call.
A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive. Here's an example. Here, when we use c in our program, it is replaced with 299792458 .
To avoid the warning and redefine the macro, undefine the macro (using #undef) first.
You can undef
ine it and define
again:
#include <iostream> #define AAA 13 int main() { #undef AAA #define AAA 7 std::cout << AAA; }
outputs: 7
Please note that statements that start with #
are preprocessor directives that are taken care of before the code is even compiled. In this case, this constant AAA
will be simply replaced by 7
, i.e. it works just like a textual replacement with no additional checks of syntax, no type safety etc...
...which is main reason why you should avoid using macros and #define
s where they can be replaced by static functions and variables :)
Why "textual replacement" ?
Look at this code:
#include <iostream> #define AAA 13 void purePrint() { std::cout << AAA; } void redefAndPrint() { #undef AAA #define AAA 7 std::cout << AAA; } int main() { #undef AAA #define AAA 4 purePrint(); redefAndPrint(); purePrint(); }
preprocessor goes line by line from the top to the bottom, doing this:
#define AAA 13
, so when I hit AAA
next time, I'll put there 13
AAA
, I'm replacing it with 13
7
, so I'll stop using 13
redefAndPrint()
I'll put there 7
transforming the given code into this one:
#include <iostream> void purePrint() { std::cout << 13; } void redefAndPrint() { std::cout << 7; } int main() { purePrint(); redefAndPrint(); purePrint(); }
which will output 13713
and the latest #define AAA 4
won't be used at all.
Something like the following:
#undef HEIGHT_TESTS #define HEIGHT_TESTS 17 // Use redefined macro // Restore #undef HEIGHT_TESTS #define HEIGHT_TESTS 13
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With