A configuration type file?

Started by
2 comments, last by silverphyre673 18 years, 10 months ago
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?
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
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 ;)
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

my siteGenius is 1% inspiration and 99% perspiration

This topic is closed to new replies.

Advertisement