How to initialize a const array class member?

F

Fred Zwarts

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.
 
J

John Harrison

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
 
F

Fred Zwarts

John Harrison said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top