Connecting Tech Pros Worldwide Forums | Help | Site Map

How to initialize a const array class member?

Fred Zwarts
Guest
 
Posts: n/a
#1: Jul 22 '05
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.

John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: How to initialize a const array class member?



"Fred Zwarts" <F.Zwarts@KVI.nl> wrote in message
news:ckgadq$9g3$1@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


Fred Zwarts
Guest
 
Posts: n/a
#3: Jul 22 '05

re: How to initialize a const array class member?



"John Harrison" <john_andronicus@hotmail.com> wrote in message news:2t1q4aF1qo36oU1@uni-berlin.de...[color=blue]
>
> "Fred Zwarts" <F.Zwarts@KVI.nl> wrote in message
> news:ckgadq$9g3$1@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.[/color]

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.
[color=blue]
>
> 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[/color]

Thanks, I'll try this suggestion.

F.Z.
Closed Thread