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?