Is init to 0 done for arrays in classes?

A

Alexander Malkis

class A {
B b[100];
public:
A() { };
//...
};

Question: are the elements of b get initialized to zero during the call
A();

Thanks in advance
Alex.

PS. To email me, remove "loeschedies" from the email address given.
 
V

Victor Bazarov

Alexander said:
class A {
B b[100];
public:
A() { };
^
The semicolon is extraneous here.
//...
};

Question: are the elements of b get initialized to zero during the call
A();

That depends on 'B's default constructor. Basically, if no other
initialisation is done, default initialisation is performed. The meaning
of default initialisation does differ depending on what 'B' is, so look
for "default initiali[s|z]ation" on Google Groups.

Victor
 
L

Leor Zolman

class A {
B b[100];
public:
A() { };
//...
};

Question: are the elements of b get initialized to zero during the call
A();

Thanks in advance
Alex.

They're initialized via calls to the B constructor (see my reply to your
other post). If, however, B is a primitive type, then they will not be
initialized. And if B's constructor doesn't do any meaningful
initialization of primitive data members of B, you'll still have garbage in
those data members.
-leor
 
A

Andrey Tarasevich

Alexander said:
class A {
B b[100];
public:
A() { };
//...
};

Question: are the elements of b get initialized to zero during the call
A();

What happens in this case depends on what 'B' is. If elements of 'B'
have default constructors, these constructors will be called. They will
do whatever they are programmed to do.

Otherwise (i.e. 'B' is not a class type), no initialization will take
place.

However, if you instantiate an 'A' object with static storage duration,
the entire object will be zero-initialized even before 'A::A()' is
executed. In that case you can expect elements of 'b' to be zeroed.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top