7

如何初始化常量整数数组类成员?我认为在同样的情况下,经典数组不是最佳选择,我应该使用什么来代替它?

class GameInstance{
    enum Signs{
        NUM_SIGNS = 3;
    };
    const int gameRulesTable[NUM_SIGNS][NUM_SIGNS]; //  how to init it?
public:
    explicit GameInstance():gameRulesTable(){};
};
4

2 回答 2

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

http://allanmcrae.com/2012/06/c11-part-5-initialization/

于 2012-11-30T13:15:50.337 回答
6

让它静态?

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}};
于 2012-11-30T12:55:59.013 回答