Less of a question, and more of a survey really. How do you define constants in your code? #define, enum or just const? What benefits are there (if any) to using any particular method?
fishybawb
Less of a question, and more of a survey really. How do you define constants in your code? #define, enum or just const? What benefits are there (if any) to using any particular method?
fishybawb
A #define is evaluated at compilation time, while a const is evaluated at run-time. Plus a lot of places find the use of #define's in C++ code bad so you should use const, but there are a few situations where #defines are helpfull, such as conditional compilation, the creation of header files, and so on.
"only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein
Mostly consts with the occasional enum if they are related, eg
Code:enum DIRECTION { LEFT = -1, NONE = 0, RIGHT = 1 };
I like const because it allows greater flexibility, const can be applied to member functions, class objects, etc, and also you can use cheats like const_cast and mutable to modify const, whereas #define leaves you stuck.