initialize constant member array?

K

Kyle

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:
const int a[4];
};

A::A():a={0,1,2,3}
{
}
 
A

Attila Feher

Kyle said:
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:
const int a[4];
};

A::A():a={0,1,2,3}
{
}

You can only initialize it inside the function body, IIRC. Eg: with a for
loop, copying from that constant array, or with memcpy - if the arrays type
allows it.
 
E

E. Robert Tisdale

Kyle said:
Is it possible to initialize a constant member array in a class?
I tried several syntax but all failed.

struct Array {
int a[4];
};

class A {
private:
// representation
const Array array;
public:
A(const Array& a): array(a) { }
~A(void) { };
};

int main(int argc, char* argv[]) {
Array array = {{0, 1, 2, 3}};
A a(array);
return 0;
}
 
K

Kyle

I just need an array with constant content. Yes, probably I need to add
static, since this array is identical for all instances. But the following
code still cannot be complied. (gcc3.3.1)
I am really confused with these 'const' problems.
class A
{
static const int a[4]={0,1,2,3};
...
};
 
K

Kyle

How to do it?
const array cannot be assigned in constructor body.
Attila Feher said:
Kyle said:
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:
const int a[4];
};

A::A():a={0,1,2,3}
{
}

You can only initialize it inside the function body, IIRC. Eg: with a for
loop, copying from that constant array, or with memcpy - if the arrays type
allows it.
 
R

Rolf Magnus

Attila said:
Kyle said:
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:
const int a[4];
};

A::A():a={0,1,2,3}
{
}

You can only initialize it inside the function body, IIRC.

No. Initialization of members can only be done in the initializer list.
Eg: with a for loop, copying from that constant array, or with memcpy
- if the arrays type allows it.

He doesn't want to copy something _from_ his array, but rather _to_ it,
which isn't possible, since it's constant.
 
A

Attila Feher

Rolf said:
Attila said:
Kyle said:
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:
const int a[4];
};

A::A():a={0,1,2,3}
{
}

You can only initialize it inside the function body, IIRC.

No. Initialization of members can only be done in the initializer
list.
Eg: with a for loop, copying from that constant array, or with memcpy
- if the arrays type allows it.

He doesn't want to copy something _from_ his array, but rather _to_
it, which isn't possible, since it's constant.

I have missed the const.
 
R

Rolf Magnus

Kyle said:
I just need an array with constant content. Yes, probably I need to
add static, since this array is identical for all instances.

If it's static, it can be initialized.
But the following code still cannot be complied. (gcc3.3.1)
I am really confused with these 'const' problems.
class A
{
static const int a[4]={0,1,2,3};
...
};

You cannot intitialize the array within the class, since you first have
to define the constant (the above only declares it), and when defining
it, you can intitialize it, so try:

class A
{
static const int a[4];
//...
};

And in the implementation file:

const int A::a[4] = {0, 1, 2, 3};
 
A

Andrey Tarasevich

Kyle said:
...
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:
const int a[4];
};

A::A():a={0,1,2,3}
{
}
...

It is only possible with a POD-class using aggregate initializer. In
your case the class is not POD which means that the array cannot be
initialized.

As a workaround, you might want to declare it as a non-constant array,
assign the values in the constructor's body and, maybe, provide a
properly const-qualified accessor function to acces the array. (It is
still not 'initialization' in the formal meaning of the word though.)
 
A

Attila Feher

Andrey said:
It is only possible with a POD-class using aggregate initializer. In
your case the class is not POD which means that the array cannot be
initialized.

As a workaround, you might want to declare it as a non-constant array,
assign the values in the constructor's body and, maybe, provide a
properly const-qualified accessor function to acces the array. (It is
still not 'initialization' in the formal meaning of the word though.)

Do you know if anyone addresses this in the work of "removing
embarrasments"?
 
K

Kyle

Thanks, Rolf,
If I need different constant values of a[] with different class instances,
and hence no static should be added, how can I initialize a[] when an object
is constructed?
class A
{
const int a[4];
...
};
Kyle
Rolf Magnus said:
Kyle said:
I just need an array with constant content. Yes, probably I need to
add static, since this array is identical for all instances.

If it's static, it can be initialized.
But the following code still cannot be complied. (gcc3.3.1)
I am really confused with these 'const' problems.
class A
{
static const int a[4]={0,1,2,3};
...
};

You cannot intitialize the array within the class, since you first have
to define the constant (the above only declares it), and when defining
it, you can intitialize it, so try:

class A
{
static const int a[4];
//...
};

And in the implementation file:

const int A::a[4] = {0, 1, 2, 3};
 
R

Rolf Magnus

Please don't top-post, and only do full quotes if neccesary.
Thanks, Rolf,
If I need different constant values of a[] with different class
instances, and hence no static should be added, how can I initialize
a[] when an object is constructed?

You can't. There is no way to initialize non-static array members in
C++. This is a shortcoming of the language.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top