*******************************************************************
It is now possible to initialize integral constant static members inside the class structure.
This is useful when the constants is used in the class structure after the initialization.
(對於當這個常量會在初始化後要使用的情況,這樣做會比較有效)
For example ===>
class MyClass{
static const int NUM = 100;
int elements[NUM];
...
};
Note that you still have to to define space for a constant static member that is initialized within a class definition:
const int MyClass::NUM; //no initialization here
*/
#pragma warning(disable:4530)
#include <iostream>
using namespace std;
class A
{
public:
A(){
for(int i=0; i<SIZE; i++)
num[i] = i;
}
void print(){
for(int i=0; i<SIZE; i++)
cout<<num[i]<<" "<<endl;
}
private:
static const SIZE = 10;
//注意:由於SIZE作為一個const常量,所以必須在首次定義的時候就給它賦值。
int num[SIZE];
};
const int A::SIZE;
//注意:由於SIZE作為一個類的靜態成員,應該在類體外部定義(以取得和全域變數類似的效用)
int main()
{
A a;
a.print();
return 0;
}