如何初始化常量整数数组类成员?我认为在同样的情况下,经典数组不是最佳选择,我应该使用什么来代替它?
class GameInstance{
enum Signs{
NUM_SIGNS = 3;
};
const int gameRulesTable[NUM_SIGNS][NUM_SIGNS]; // how to init it?
public:
explicit GameInstance():gameRulesTable(){};
};
如何初始化常量整数数组类成员?我认为在同样的情况下,经典数组不是最佳选择,我应该使用什么来代替它?
class GameInstance{
enum Signs{
NUM_SIGNS = 3;
};
const int gameRulesTable[NUM_SIGNS][NUM_SIGNS]; // how to init it?
public:
explicit GameInstance():gameRulesTable(){};
};
在 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
让它静态?
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}};