Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the value of a macro at run time

I'm working in Visual Studio 2010, using C++ code. What I'm trying to do is change the value of a preprocessor directive during run time, not sure if it's possible but I've 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 POO be changed so that class objects use a different implementation of other methods? Or if it's not possible, what's another approach to this?

like image 937
GelatinFox Avatar asked Oct 25 '13 03:10

GelatinFox


1 Answers

You seem to be confused about the nature of "preprocessor" directive. These only exist before compiler processing. The compiler eliminates (replaces/processes) macro definitions during the compile step. They don't exist at runtime to change. It's actually a mini-language unto itself that only compiles 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 sort of runtime input. This may indicate a design problem. You might consider defining two different classes (possibly with a common trivial base class).

like image 70
Dweeberly Avatar answered Nov 11 '22 09:11

Dweeberly