可能重复:
initialize a const array in a class initializer in C++
这是一个新手问题。如何初始化常量整数数组类成员?我认为在相同的情况下,经典数组不是最佳选择,我应该使用什么而不是它呢?
class GameInstance{
enum Signs{
NUM_SIGNS = 3;
};
const int gameRulesTable[NUM_SIGNS][NUM_SIGNS]; // how to init it?
public:
explicit GameInstance():gameRulesTable(){};
};
答案 0 :(得分:7)
在C ++ 11中,您可以在初始化列表中初始化const数组成员
class Widget {
public:
Widget(): data {1, 2, 3, 4, 5} {}
private:
const int data[5];
};
或
class Widget {
public:
Widget(): data ({1, 2, 3, 4, 5}) {}
private:
const int data[5];
};
有用的链接:http://www.informit.com/articles/article.aspx?p=1852519
答案 1 :(得分:5)
让它静止吗?
class GameInstance{
enum Signs{
NUM_SIGNS = 3};
static const int gameRulesTable[2][2];
public:
explicit GameInstance(){};
};
...in your cpp file you would add:
const int GameInstance::gameRulesTable[2][2] = {{1,2},{3,4}};