By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Is init to 0 done for arrays in classes?

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Tue, 18 May 2004 18:43:20 +0200, Alexander Malkis
<al*****************@stone.cs.uni-sb.de> wrote:
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

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


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2

P: n/a
Alexander Malkis wrote:
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
Jul 22 '05 #3

P: n/a
Alexander Malkis wrote:
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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.