Initializing an array member const array

Possible duplicate:
initialize const array in class initializer in C ++

This is a newbie question. How to initialize a constant integer class of a class? I think that in the same case, the classic array is not the best choice, what should I use instead?

class GameInstance{ enum Signs{ NUM_SIGNS = 3; }; const int gameRulesTable[NUM_SIGNS][NUM_SIGNS]; // how to init it? public: explicit GameInstance():gameRulesTable(){}; }; 
+7
source share
2 answers

Make it static?

 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}}; 
+5
source

In C ++ 11, you can initialize the const array member in the initialization list

 class Widget { public: Widget(): data {1, 2, 3, 4, 5} {} private: const int data[5]; }; 

or

 class Widget { public: Widget(): data ({1, 2, 3, 4, 5}) {} private: const int data[5]; }; 

useful link: http://www.informit.com/articles/article.aspx?p=1852519

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

+6
source

All Articles