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

initialize an array member

Hi,

Do you know how can I intialize an array member:
for example
class A
{
public:
A(int x);
};

class B
{
public:
B();
private:
A a[2];
};

how do I intialize a?
Thanks

Avner

Jul 22 '05 #1
4 2271
Avner Flesch wrote:
Hi,

Do you know how can I intialize an array member:
for example
class A
{
public:
A(int x);
};

class B
{
public:
B();
private:
A a[2];
};

how do I intialize a?


You can't. C++ doesn't offer a way to initialize array members in the
initializer list. You have to get the members default-constructed and
then set whatever you need in the constructor body.

Jul 22 '05 #2

"Avner Flesch" <av***@il.marvell.com> wrote in message
news:3F**************@il.marvell.com...
Hi,

Do you know how can I intialize an array member:
for example
class A
{
public:
A(int x);
};

class B
{
public:
B();
private:
A a[2];
};

how do I intialize a?


Same way you initialize any other member(s), with a constructor.

B::B()
{
a[0] = A(1);
a[1] = A(2);
}

This isn't technically 'initialization', it's assignment, but
its the best that can be done with an (non static) array member.

The language does not provide an initializer list syntax
for arrays. Maybe consider a container (or perhaps a std::pair)
instead.

-Mike
Jul 22 '05 #3
Mike Wahler wrote:

"Avner Flesch" <av***@il.marvell.com> wrote in message
news:3F**************@il.marvell.com...
Hi,

Do you know how can I intialize an array member:
for example
class A
{
public:
A(int x);
};

class B
{
public:
B();
private:
A a[2];
};

how do I intialize a?
Same way you initialize any other member(s), with a constructor.


The difference is that array members can only be default initialized.
B::B()
{
a[0] = A(1);
a[1] = A(2);
}

This isn't technically 'initialization', it's assignment, but
its the best that can be done with an (non static) array member.
"technically" or not. It's not initialization. The initialization
happend before the constructor of B was entered.
The language does not provide an initializer list syntax
for arrays. Maybe consider a container (or perhaps a std::pair)
instead.


I guess if the number was only 2 anyway, just two separate member
variables would be the easiest way.

Jul 22 '05 #4
I don't think there is a way to initialize an array with anything
other than a default constructor.

You could change the array to an array of pointers and then fill
in the array by new for individual objects at run-time.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF
Jul 22 '05 #5

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

Similar topics

13
by: Kyle | last post by:
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:
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 ...
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...
10
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
15
by: thinktwice | last post by:
char a = { 0 } is it ok?
2
by: heng | last post by:
If the data member of a class is an array, how to initialize? I tried the following, but it is wrong. class A { public: int a; A():a({0,0,0}){} };
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
11
by: Bob Altman | last post by:
Hi all, I have a class that contains a member variable that is an array of class instances: class MyClass { private: SomeClass m_someClass; SomeClass m_arrayOfClasses; };
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: 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.