Hi,
I found myself in this situation and my reference book doesn't give a good answer and searching on google has yield not so desirable result. So I have an array that every object of the class will use, so I want to make that array "static const", so every object of the same class doesn't have to construct that array and these objects can't alter any part of the array.
One of the answer is only shows how to initialize a static array:
This code doesn't work for "static const int array[20]". The compiler would protest. And since this is of type "int array[]" and not an integral type, the follow method would not work either:Code:// example of static array private member initialization class A { private: static int array[20] =; public: static void setArray(); ... }; int A::array[] = {0}; void A::setArray() { for(int i = 0; i < 20; i++) array[i] = i; }
This seems difficult because static variable can't be initialized in a constructor, and const variable has to be initialized in the constructor. And initialization of " static const" only works for integral types.Code:class A { private: static const int array[20] = {1,2,3,...20}; public: ... };
Do any one knows a work-around? Or I just have to be careful and not alter alter any part of the static array?