473,766 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14841
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**********@h otmail.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******@mkwah ler.net> дÈëÓʼþ
news:AK******** **********@news read2.news.pas. earthlink.net.. .
"Kyle" <zh**********@h otmail.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**********@l mf.ericsson.se> дÈëÓʼþ
news:bv******** **@newstree.wis e.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

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

Similar topics

6
6450
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 array with two dimension. I've tried the following but seems the syntax is somehow wrong. Can someone help me in doing so or any clue would be fully appreciate. Here is my bugged code:
4
2300
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
5936
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
15
5323
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 private string myArray;
4
2558
by: bob | last post by:
Why doesn't c++ let me initialize static members like this: class MySound { static CSoundManager* g_pSoundManager = NULL; };
6
19175
by: nik | last post by:
hello friends, a class having two const variables ,how can we initialize these variables
18
4215
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 use my specific constructor to initialize the class. How to do it without using malloc? Something like Point* pt = new Point; I want it to reserve the space for N points only, and not to call default constructor. I dont have a default...
15
26435
by: thinktwice | last post by:
char a = { 0 } is it ok?
7
2813
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 naming names here to remove bias - I'm trying to tell if I'm relying on implementation defined behavior or if this is a bug in the new compiler. Consider this stripped down example:
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.