Change macro value at runtime

I work in Visual Studio 2010 using C ++ code. What I'm trying to do is change the value of the preprocessor directive at runtime, not sure if this is possible, but I tried this.

somefile.h

static int mValue = 0; #define POO = mValue; ... #if POO 0 //define class methods #else //define class methods differently } 

main.cpp

 main() { //Code calls constructor and methods allowed when POO is 0 //Code increments mValue //Code calls constructor and methods allowed when POO is 1 } 

How can I change POO so that class objects use a different implementation of other methods? Or, if this is not possible, what is the other approach to this?

+6
source share
1 answer

You seem to be confused with the nature of the "preprocessor" directive. They exist only before compiler processing. The compiler eliminates (replaces / processes) macro definitions during the compilation phase. They do not exist at the time the changes are made. This is actually a mini-language for itself that compiles only into c / C ++ code, which is then processed by the compiler.

It sounds like you want your class to be two different things based on some input at runtime. This may indicate a design problem. You might consider defining two different classes (perhaps with a common trivial base class).

+8
source

Source: https://habr.com/ru/post/956690/


All Articles