• Advertisement
Sign in to follow this  

A configuration type file?

This topic is 4648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would i like Redefine the values of #defines? like say i do #define hey 12 but later on something happens and the program needs to make hey=15?

Share this post


Link to post
Share on other sites
Advertisement
you can't.

#defines are processed _before_ the compiler even compiles your code. Basically it does a search and replace before it compiles, replacing every instance of hey with 12. so it is impossible to change as "hey" never actually exists in your program. If you want a mutable value, then use a global variable that gets changed programatically.

-me

Share this post


Link to post
Share on other sites
In case you'd do it per CPP file, you can still

#undef hey
#define hey 15

Remember that depending the order in which the files are compiled hey will still hold the last value it was defined to...

You should probably follow Palidine's advice ;)

Share this post


Link to post
Share on other sites
This isn't what you should be using it for, though. The preprocessor looks through your code and cuts out all mentions of 'hey' and replaces them with 12, or 15, or whatever. You can't re-define this very well, and it will be much cleaner and easy to understand if you just use a global integer. #define statements are mostly used for inclusion guards, and things like PI, which are constants and never change. However, I usually would just make PI a const float, for clarity and consistency:

#define PI 3.141592

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement