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.
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 constexpr
keyword which allows you to do the same for a much wider variety of types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With