Here is my class:
class MyClass {
public:
MyClass();
~MyClass();
private:
// non-static const array as a data member
const int memArr_[5];
};
Is such a class (with non-static const array as a data member) valid?
Since I cannot see why it may not be valid, I presume that it is valid.
Therefore when an object of MyClass is constructed, memArr_[5], being a
const embedded object, will need to get initialized in the constructor.
My problems start here as I cannot write a constructor that has the
right initializer for the above array. Every attempt gives a
compilation error (the errors reported below are from VC++ 7.1 - but
I get similar errors in VC++ 6.0 and GCC 3.2.x on Linux).
Attempt 1:
==========
MyClass::MyClass():
memArr_(0, 1, 2, 3, 4)
// error C2536: 'MyClass::MyClass::c_memArr_' :
// cannot specify explicit initializer for arrays
{ }
Attempt 2:
==========
MyClass::MyClass():
memArr_({0, 1, 2, 3, 4})
// error C2958: the left parenthesis '('
// found at '...\myclass.hxx(17)' was
// not matched correctly
{ }
Attempt 3:
==========
MyClass::MyClass()
// error C2439: 'MyClass::c_memArr_' :
// member could not be initialized
{ }
Can anyone tell me if I am missing something here and show me how to
write the initializer correctly? Or if this is wrong according to C++
standard and why (I cannot convince myself why should this be wrong)?
Or is this a compiler limitation?