为什么我不能在类中初始化非const静态成员或静态数组?
为什么我不能在类中初始化非const static
成员或static
数组?
class A{ static const int a = 3; static int b = 3; static const int c[2] = { 1, 2 }; static int d[2] = { 1, 2 };};int main(){ A a; return 0;}
编译器发出以下错误:
g++ main.cpp main.cpp:4:17: error: ISO C++ forbids in-class initialization of non-const static member ‘b’main.cpp:5:26: error: a brace-enclosed initializer is not allowed here before ‘{’ token main.cpp:5:33: error: invalid in-class initialization of static data member of non-integral type ‘const int [2]’main.cpp:6:20: error: a brace-enclosed initializer is not allowed here before ‘{’ token main.cpp:6:27: error: invalid in-class initialization of static data member of non-integral type ‘int [2]’
我有两个问题:
为什么我不能static
在课堂上初始化数据成员?
为什么我不能static
在类中初始化数组,甚至是const
数组?
Helenr
GCT1015
精慕HU
相关分类