As we know,It is possible to initialize integral const static members inside the class structure.This is useful when the constant is used in the class structure after the initialization.For example,it can be used as the size of an int array.
Look the code following:
class MyClass{
static const int num = 100;
int elems[num];
...
};
But we still have to define the member num outside the class definition:
const int MyClass::num;
I don't know why we have to do like this.
Could someone tell me why?
Thanks a lot.
In addition,I write the following code:
#include <iostream>
using namespace std;
class MyClass{
public:
MyClass()
{
cout << "instruct class MyClass!" << endl;
}
static const int num = 100;
int elems[num];
};
//const int MyClass::num;
int main()
{
MyClass a;
const int *b = &(a.num);
cout << "&(a.num): " << &(a.num) << endl;
cout << "a.num: " << a.num << endl;
cout << "*b: " << *b << endl;
}
It runs well on Visual Studio 2008:
But I have removed the code that definite the member num outside the class.
I am very confused.Could someone interpret it for me?
The initialization in the class is mainly used to obtain a constant expression. For this only the value matters. Once you take the address of the object or bind it to a reference, the compiler needs a location for the object as well. This is effectively what the definition provides.
You would need to define the static constant num
outside the class in a cpp file only if your code takes it's address.This is known as an Out-of-class definition.
If your code does not take address of num
, then the In-class Initialization would just work fine.
Rationale:
Bjarne states:
"C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects."
Note that only static const
integers can be treated as compile time constants. The compiler knows that the integer value will not change anytime and hence it can apply its own magic and apply optimizations, the compiler simply inlines such class members i.e, they are not stored in memory anymore, As the need of being stored in memory is removed, it gives such variables the exception to the above rule mentioned by Bjarne.
Even if static const
integral values can have In-Class Initialization, taking address of such variables is not allowed. One can take the address of a static member if (and only if) it has an out-of-class definition because then the compiler needs to place them in memory.