473,387 Members | 1,540 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,387 software developers and data experts.

How to initialize a const array class member?

If I am right, members of a class that are const and not static
must be initialized in the initialization part of a constructor.
E.g.

class C {
private:
const int I;
public:
C();
};

This class requires that the constructor initializes I in the
initialization part. E.g.:

C::C () : I(5) {}

How is this done for a const array, e.g., if C is modified as:

class C {
private:
const int I[3];
public:
C();
};
How is the array I initialized?
I cannot find the syntax for initializing the array.
Of course, the question does not only apply to int types,
but also to more complex (class) types.

F.Z.
Jul 22 '05 #1
2 5551

"Fred Zwarts" <F.******@KVI.nl> wrote in message
news:ck**********@info.service.rug.nl...
If I am right, members of a class that are const and not static
must be initialized in the initialization part of a constructor.
E.g.

class C {
private:
const int I;
public:
C();
};

This class requires that the constructor initializes I in the
initialization part. E.g.:

C::C () : I(5) {}

How is this done for a const array, e.g., if C is modified as:

class C {
private:
const int I[3];
public:
C();
};
How is the array I initialized?
I cannot find the syntax for initializing the array.
Of course, the question does not only apply to int types,
but also to more complex (class) types.

F.Z.

You cannot explicitly initialise an non-static const array in a class.

But if the array is const why not make it static? I can't think of any
obvious reason why not.

If you really needed this for some reason then I would do something like
this

struct A
{
A() { i[0] = 1; i[1] = 2; i[2] = 3]; }
int i[3];
};

class C {
private:
const A a;
public:
C();
};

john
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message news:2t*************@uni-berlin.de...

"Fred Zwarts" <F.******@KVI.nl> wrote in message
news:ck**********@info.service.rug.nl...
If I am right, members of a class that are const and not static
must be initialized in the initialization part of a constructor.
E.g.

class C {
private:
const int I;
public:
C();
};

This class requires that the constructor initializes I in the
initialization part. E.g.:

C::C () : I(5) {}

How is this done for a const array, e.g., if C is modified as:

class C {
private:
const int I[3];
public:
C();
};


How is the array I initialized?
I cannot find the syntax for initializing the array.
Of course, the question does not only apply to int types,
but also to more complex (class) types.

F.Z.

You cannot explicitly initialise an non-static const array in a class.

But if the array is const why not make it static? I can't think of any
obvious reason why not.
Two reasons:

1) The static initialization order fiasco.
For int type probably not relevant, but for more complex types I want to
avoid static initialization.

2) Maybe each object initializes the array with different values,
which do not change during the live time of the object, but may be
different when the next object of the class is initialized.

If you really needed this for some reason then I would do something like
this

struct A
{
A() { i[0] = 1; i[1] = 2; i[2] = 3]; }
int i[3];
};

class C {
private:
const A a;
public:
C();
};

john


Thanks, I'll try this suggestion.

F.Z.
Jul 22 '05 #3

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

Similar topics

13
by: Kyle | last post by:
Hi, Is it possible to initialize a constant memeber array in a class? I tried several syntax but all failed. class A { public: A(); ~A(); private:
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
7
by: Bala L | last post by:
I have a class with a private array data member 'm_array'. I have written a public function, called 'fileRead', to read values into the array from a file. I just noticed that I have declared this...
2
by: heng | last post by:
If the data member of a class is an array, how to initialize? I tried the following, but it is wrong. class A { public: int a; A():a({0,0,0}){} };
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
9
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.