Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can a class have a non-static const array as a data member?

Reply
Thread Tools

Can a class have a non-static const array as a data member?

 
 
partha.p.das@gmail.com
Guest
Posts: n/a
 
      04-04-2006
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?

 
Reply With Quote
 
 
 
 
asterisc
Guest
Posts: n/a
 
      04-04-2006
Why don't you use std::vector?

 
Reply With Quote
 
 
 
 
Mark P
Guest
Posts: n/a
 
      04-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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).
>


My understanding is that there's nothing which prohibits you from having
such a member however, since it must be initialized in the ctor, its
usefulness is quite limited. As you discovered, lists (with or without
brackets) are not valid initializers. The only valid initialization I
know of is memArr_() which will default initialize all of the array
elements (0 for built-in types). Or you can not explicitly initialize
it and take whatever junk the compiler gives you.

Mark
 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      04-04-2006
Mark P <(E-Mail Removed)> wrote:
> The only valid initialization I
> know of is memArr_() which will default initialize all of the array
> elements (0 for built-in types).


However, be aware that at least one popular compiler (VS .NET 2003, aka
VC++ 7.1) does not do this correctly:

http://groups.google.com/group/comp....b44a8b786b6a79

and read the response by Dietmar Kuehl also.


However, they did fix it in the new version (VS .NET 2005, aka VC++
8.0), and Patrick Kowalzick has also posted a work-around for the older
version:

http://groups.google.com/group/micro...a339bdd2c2aa6e

--
Marcus Kwok
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      04-04-2006

(E-Mail Removed) wrote:
> 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.


It's valid. Consider that the elements could be of class type. In that
case, they will be nicely initialized by their default constructors.

Also, an array could be wrapped in a struct. The const qualifier could
be put on the struct member instead of the array, like this:

const struct Foo {
int array[5];
} s; // s is the non-static member of the class

So now you can initialize the thing in the constructor, because what
you are initializing is a struct object, and not an array member.

MyClass::MyClass(Foo init)
: s(init)
{
}

Or:

// static member function
MyClass::Foo MyClass::makeFooStruct( ... args ...)
{
}

MyClass::MyClass()
: s(makeFooStruct(... args ...))
{
}

I.e. you write some function that constructs a Foo according to some
parameters, and then call it in your constructor's initializing
expression.

 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      04-04-2006
Marcus Kwok wrote:
> Mark P <(E-Mail Removed)> wrote:
>> The only valid initialization I
>> know of is memArr_() which will default initialize all of the array
>> elements (0 for built-in types).

>
> However, be aware that at least one popular compiler (VS .NET 2003, aka
> VC++ 7.1) does not do this correctly:
>


A good point, and in fact, not the only compiler with this defect. I've
seen problems with a version of the Sun CC compiler too.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments