Possible Duplicate:
static const vs #define
In C++, to define a constant to be used across the entire application, what is the usual practice?
#define WINDOWS_HEIGHT 1024
or
const int WINDOWS_HEIGHT = 1024;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Pros and cons to everything, depending on usage:
template <typename T> void f(T t) { cout << ++t; }won’t compile)template <typename T> void f(T)get a distinct instantiation when passed the same numeric value from different enums, all of which are distinct from any actual f(int) instantiation.#define X "x"and some client usage ala"pre" X "post", you’re in trouble if you want or need to make X a runtime-changeable variable rather than a constant, whereas that transition is easier from aconst char*orconst std::stringgiven they already force the user to incorporate concatenation operations.{ 1, 2 }that can be used to initialise arrays, or#define MICROSECONDS *1E-6etc. (definitely not recommending this!)__FILE__and__LINE__can be incorporated into the macro substitutionAs a general rule, I use consts and consider them the most professional option for general usage (though the others have a simplicity appealing to this old lazy programmer).