不能在类里直接初始化const数据成员,const数据成员属于某个对象,不同对象const数据成员的值可能不同
class Base {
const int SIZE = 10; // error
int array[SIZE]; // error
}
const数据成员只能在类的构造函数的初始化列表中进行初始化
class Base {
public:
Base(int size) : SIZE(size) {}
private:
const int SIZE;
}
可以使用enum创建属于类的常量
class Base {
enum{size = 100};
int array[size];
}