473,287 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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

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?

Apr 4 '06 #1
5 2441
Why don't you use std::vector?

Apr 4 '06 #2
pa**********@gmail.com 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
Apr 4 '06 #3
Mark P <us****@fall2005remove.fastmailcaps.fm> 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
Apr 4 '06 #4

pa**********@gmail.com 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.

Apr 4 '06 #5
Marcus Kwok wrote:
Mark P <us****@fall2005remove.fastmailcaps.fm> 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.
Apr 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
2
by: Capstar | last post by:
Hi NG, Is it possible to make a template class, which has only one method that makes use of the template type. The rest of the methods will work on the base class of which the template type...
10
by: Matthias | last post by:
Hello, I thought one major advantage of using functors as e.g. sorting predicates over functions would be that I can do something like this: void foo() { class Predicate { public:
3
by: Parvesh | last post by:
hi, I am using a webservice which Returns the Result in an XML string, The XML response i get i svery cumbersome to parse, But if i could convert it to the Corresponding Class using the...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
2
by: flopbucket | last post by:
Hi, If I have the following: template<class T> class A { public: int a; };
1
by: bytebugs | last post by:
Hi, As a matter of principle I have refranied from creating an instance of Form or UserControl from within a class that does not inherit from System.Windows.Form. I am looking for comments on...
5
by: The Cool Giraffe | last post by:
I'm designing an ABC and in connection to that i have run into some "huh!" and "oh...". Let me put it as a list. 1. Since the class will only contain bodies of the methods, only the header file...
15
by: Anthony Greene | last post by:
This is probably a very introductory object-oriented question, but it has been nagging me for years, and since I've never been able to find the right answer, I've had to work around it with...
5
by: Sin Jeong-hun | last post by:
class Engine { Thread Worker; public event ... EngineMessage; public void Start() { Worker=new Thread(new ThreadStart(Run)); Worker.Start(); } private Run()
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.