Alternatives to using "#define" in C++? Why is it frowned upon?
c++, c-preprocessor, coding-style, global-variables
Solution
Why is this this code bad?
Because VERSION can be overwritten and the compiler won't tell you.
Is there an alternative to using #define?
const char * VERSION = "1.2";
or
const std::string VERSION = "1.2";
Problem
I have been developing C++ for less than a year, but in that time, I have heard multiple people talk about how horrible `#define` is. Now, I realize that it is interpreted by the preprocessor instead of the compiler, and thus, cannot be debugged, but is this really that bad? Here is an example (untested code, but you get the general idea): ``` #define VERSION "1.2" #include <string> class Foo { public: string getVersion() {return "The current version is "+VERSION;} }; ``` - Why is this this code bad? - Is there an alternative to using `#define`?