1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> using namespace std; class demo { public: demo(){} private: const int m_matrix[10][10]; }; int main() { return 0; } |
error C2439: ‘demo::m_matrix’ : member could not be initialized
这可咋办啊?class里面,普通数组可以有,可const数组就真的不能有吗?
你好!除了代码,此处没有多少原创之物,皆为本人搜集、整理、总结之记录与心得,欢迎转载分享!转载时请尽量注明出处,将不胜感激。祝你健康、快乐!
一个对象拥有常量成员数组没啥意义啊,而且浪费空间。
取而代之的是,class本身可以有常量数组。
#include
using namespace std;
class T{
const static int a[3] = {1,2,3};
};
const int T::a[3] = {1,2,3};
int main(){
return 0;
}
啊。写错了。在类里面不能初始化。要在外面初始化。
这样:
class T{
const static int a[3];
};
const int T::a[3] = {1,2,3};
这个我倒是知道…… 可为什么”没啥意义呢”?