Possible Duplicate:
Why aren't static const floats allowed?
Is there any reason why this is not possible in C++? It confuses me.
static const int A = 100; //no error
static const float B = 2.0f; //error, can't define this type in class definition.
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.
Static constants of integral types can be initialized inside a class definition. That doesn’t mean that the object actually exists, since you haven’t provided a definition yet, but because the compiler knows the value of the object, you can sometimes get away with it.
That is, if you’re not attempting to take the address of the variable or pass it by reference, but only use its value, then you don’t need to provide a defintion at all, and the compiler simply substitutes the value wherever you use the variable.
C++11 introduces the
constexprkeyword which allows you to do the same for a much wider variety of types.