Object Initialization Question

B

Bob Evans

If class members (attributes) are not explicitly initialized in a
constructor, are they zero-initialized OR are their contents undefined
when the object is constructed? Is this specified in the langauge
standard?

My guess is: they are undefined.

Thanks, Bob
 
V

Victor Bazarov

Bob said:
If class members (attributes) are not explicitly initialized in a
constructor, are they zero-initialized OR are their contents undefined
when the object is constructed? Is this specified in the langauge
standard?
Yes.

My guess is: they are undefined.

The Standard says that the behaviour is to default-initialise the members
that are not mentioned in the initialisation list IFF those entities are
base classes or non-static members of a class (non-POD) type. If the
omitted member is of a POD type ("or array thereof"), it is left
uninitialised.

Example:

struct A {
int a;
std::string s;
A() {}
};

A a;

here 'a.a' has indeterminate value, whereas 'a.s' is an empty string.

V
 
A

Andrey Tarasevich

Bob said:
If class members (attributes) are not explicitly initialized in a
constructor, are they zero-initialized OR are their contents undefined
when the object is constructed?

The answer depends on the following details:

1) type of the member (POD, aggregate, non-POD)
2) certain properties of the constructor of the enclosing class
(explicitly declared or not)
3) intializer used for the object of the enclosing class
Is this specified in the langauge standard?
Yes.

My guess is: they are undefined.

Not enough information. Would you please provide more concrete details?
 
R

Ron Natalie

Bob said:
If class members (attributes) are not explicitly initialized in a
constructor, are they zero-initialized OR are their contents undefined
when the object is constructed? Is this specified in the langauge
standard?

My guess is: they are undefined.

If the member is of non-POD type, it is default initialized.
If the class type is of POD type, and the storage duration
is static, it's zero-initialized, if it's auto, it's undefined,
if it's allocated dynamically, it depends on the form of the call
to new.

Yes it is rediculously inconsistent
 
J

JKop

If the member is of non-POD type, it is default initialized.
If the class type is of POD type, and the storage duration
is static, it's zero-initialized, if it's auto, it's undefined,
if it's allocated dynamically, it depends on the form of the call
to new.

Yes it is rediculously inconsistent.


It's strange: When you read the above, it seems so complicated... but
after time you just get used to it!

I'll just stick to my good ol' "ValueInitialized" template for the time
being!


-JKop
 
B

Bob Evans

Thanks to all respondents. It is sufficiently confusing that I shall
always initialize all my non-static members in my constructors. This
sounds like a "best practice" to me anyway. In some cases it may make
for a long initialization list.
Bob.
 
V

Victor Bazarov

Bob Evans said:
Thanks to all respondents. It is sufficiently confusing that I shall
always initialize all my non-static members in my constructors. This
sounds like a "best practice" to me anyway. In some cases it may make
for a long initialization list.

It's not always possible, just so you know. For example, there is no way
to initialise arrays or other aggregates.
Bob.

Ron Natalie said:
Bob said:
[.. I guess you didn't really mean to quote it so I'll remove it ..]
 

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
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top