Initializing an Array inside a Class?
-
Hi everyone! Got a quick, yet stupid question! Why cant you init anything inside a class unless its
static const int
? Actualy the question is how can you initialize an array insideclass private:
to beconst
. I know that it is possible to declare an array and then use a constructor to init it. But how do I make itconst
if I was to do it thru the constructor. Thanks. -
Hi everyone! Got a quick, yet stupid question! Why cant you init anything inside a class unless its
static const int
? Actualy the question is how can you initialize an array insideclass private:
to beconst
. I know that it is possible to declare an array and then use a constructor to init it. But how do I make itconst
if I was to do it thru the constructor. Thanks.You can declare an array inside private block, e.g. class A { private: static const int array[10]; }; and initialize outside class as: const A::array[10] = {0}; As far as I know, you cannot initialize an const array using constructor list. Arsalan Malik
-
You can declare an array inside private block, e.g. class A { private: static const int array[10]; }; and initialize outside class as: const A::array[10] = {0}; As far as I know, you cannot initialize an const array using constructor list. Arsalan Malik
Although the former is valid C++ syntax, I think the VC6 compiler will have issues with it.
-
Although the former is valid C++ syntax, I think the VC6 compiler will have issues with it.
I have successfully compiled this syntax with VC6, and found no issues. Arsalan Malik
-
You can declare an array inside private block, e.g. class A { private: static const int array[10]; }; and initialize outside class as: const A::array[10] = {0}; As far as I know, you cannot initialize an const array using constructor list. Arsalan Malik
Thanks a lot!!! :-D