vendredi 22 juillet 2016

Alternatives to using "#define" in C++? Why is it frowned upon?

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;}
};
  1. Why is this this code bad?
  2. Is there an alternative to using #define?

Aucun commentaire:

Enregistrer un commentaire