常量数组类成员初始化

7
如何初始化常量整型数组类成员?我认为在某些情况下,传统的数组并不是最好的选择,那么我应该使用什么来代替它呢?
class GameInstance{
    enum Signs{
        NUM_SIGNS = 3;
    };
    const int gameRulesTable[NUM_SIGNS][NUM_SIGNS]; //  how to init it?
public:
    explicit GameInstance():gameRulesTable(){};
};

谢谢您的评论,我查看了这个问题,但是那些问题在2008年就已经得到了回答,并且有一些关于可能的新标准功能的讨论,这些功能后来在2011年被接受。 - vard
3
那个问题中有关于C++11的答案。 - Joseph Mansfield
1
@vard 这是Flexo的答案。 - Angew is no longer proud of SO
2个回答

7

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}};

很遗憾,我的编译器不允许这样做。他说有多个数组声明。 - vard
2
@vard 'const int GameInstance::gameRulesTable[2][2] = {{1,2},{3,4}};' - 这应该放在.cpp文件中。 - Stals

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接