Suppose class Myclass { private: static const int myarray[2]; }
If I wanted to initialize myarray I should put the following statement in global scope:
const int Myclass::myarray[2] = {1,1};
What should I do If I want to initialize my array in main() (at some runtime calculated values eg at {n1, n2} where n1 and n2 are values calculated at runtime in main() based on the command line arguments)
There’s nothing much you can do.
You could create a member function that would initialize the values, and call it. But, if it’s
static,privateandconst– then you can’t even do that and out of options.You cannot initialize a
staticmember at run-time, you cannot access aprivatemember from outside of class (unless you make friends), and you cannot change aconstmember once initialized.If you give up
const, then you can change it. You still have to initialize at global scope, but you can change values.Note that as long as its
private, you won’t be able to access it frommain, but you can write a wrapper function member to do that for you (or make itpublic).