C ++ macro - header line

I use preprocessor macros to declare some duplicate variables, in particular:

QuitCallbackType quitCallback; LossCallbackType lossCallback; PauseCallbackType pauseCallback; KeyCallbackType keyCallback; MouseCallbackType mouseCallback; 

I would like to use a preprocessor macro for this, a la

 CREATE_CALLBACK_STORAGE(quit) CREATE_CALLBACK_STORAGE(loss) CREATE_CALLBACK_STORAGE(pause) CREATE_CALLBACK_STORAGE(key) CREATE_CALLBACK_STORAGE(mouse) 

where it will look like this:

 #define CREATE_CALLBACK_STORAGE(x) capitalize(x)##CallbackType x##CallBack; 

Is there a way to do this, so I don’t need to pass both uppercase and lowercase versions of each name?

I understand that there is not much to type for using macros, but the problem itself has begun to intrigue me.

+6
c ++ macros
source share
2 answers

A macro preprocessor cannot take substrings or uppercase letters. Unfortunately.

If you can change your naming scheme, you may have more success. For example:

 QuitCallbackType _QuitCallback; 

Strike>

Edit: I am warned not to use underscores, but the idea still applies:

 QuitCallbackType callbackQuit; 
+4
source share

I think you should completely abandon the idea of ​​macros. A better solution would be to create a simple data structure, for example:

 struct CallBacks { QuitCallbackType quit; LossCallbackType loss; PauseCallbackType pause; KeyCallbackType key; MouseCallbackType mouse; }; 

And use instead:

 CallBacks callback; 

You can use only those members that you want:

 callback.quit = GetCallback(...); someFunc(callback.quit); // ect.. 

It also makes variable names (in my opinion) a little clearer.

+2
source share

All Articles