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

initialize constant member array?

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}
{
}
Jul 22 '05 #1
13 14787
Kyle wrote:
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.

--
Attila aka WW
Jul 22 '05 #2
"Kyle" <zh**********@hotmail.com> wrote in message
news:40********@dnews.tpgi.com.au...
Hi,
Is it possible to initialize a constant memeber array in a class?


No, not a nonstatic one. Why do you want to?

-Mike
Jul 22 '05 #3
Kyle wrote:
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;
}

Jul 22 '05 #4
Attila Feher wrote:
Kyle wrote:
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.
Jul 22 '05 #5
Rolf Magnus wrote:
Attila Feher wrote:
Kyle wrote:
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.

--
Attila aka WW
Jul 22 '05 #6
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};
...
};

"Mike Wahler" <mk******@mkwahler.net> дÈëÓʼþ
news:AK******************@newsread2.news.pas.earth link.net...
"Kyle" <zh**********@hotmail.com> wrote in message
news:40********@dnews.tpgi.com.au...
Hi,
Is it possible to initialize a constant memeber array in a class?


No, not a nonstatic one. Why do you want to?

-Mike

Jul 22 '05 #7
How to do it?
const array cannot be assigned in constructor body.
"Attila Feher" <at**********@lmf.ericsson.se> дÈëÓʼþ
news:bv**********@newstree.wise.edt.ericsson.se...
Kyle wrote:
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.

--
Attila aka WW

Jul 22 '05 #8
Kyle wrote:
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};

Jul 22 '05 #9
Kyle wrote:
How to do it?
const array cannot be assigned in constructor body.


I have missed the const.

--
Attila aka WW
Jul 22 '05 #10
Kyle wrote:
...
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.)

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #11
Andrey Tarasevich wrote:
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"?

--
Attila aka WW
Jul 22 '05 #12
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" <ra******@t-online.de> ????
news:bv*************@news.t-online.com...
Kyle wrote:
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};

Jul 22 '05 #13
Please don't top-post, and only do full quotes if neccesary.

Kyle wrote:
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.
Jul 22 '05 #14

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

Similar topics

6
by: sruojlim | last post by:
Hi everyone I'm posting again this question as my system was down at time of the first posting...well something in the newsgroups was down. I'd like to know how i should declare a constant as an...
4
by: Avner Flesch | last post by:
Hi, Do you know how can I intialize an array member: for example class A { public: A(int x); };
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
4
by: bob | last post by:
Why doesn't c++ let me initialize static members like this: class MySound { static CSoundManager* g_pSoundManager = NULL; };
6
by: nik | last post by:
hello friends, a class having two const variables ,how can we initialize these variables
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...
15
by: thinktwice | last post by:
char a = { 0 } is it ok?
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
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: 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: 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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.