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

Object Initialization Question

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
Jul 22 '05 #1
6 1233
Bob Evans wrote:
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
Jul 22 '05 #2
Bob Evans wrote:
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?

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #3
Bob Evans wrote:
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
Jul 22 '05 #4
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
Jul 22 '05 #5
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.

Ron Natalie <ro*@sensor.com> wrote in message news:<41***********************@news.newshosting.c om>...
Bob Evans wrote:
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

Jul 22 '05 #6
"Bob Evans" <bt**********@yahoo.com> wrote...
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 <ro*@sensor.com> wrote in message
news:<41***********************@news.newshosting.c om>...
Bob Evans wrote:
> [.. I guess you didn't really mean to quote it so I'll remove it ..]

Jul 22 '05 #7

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

Similar topics

106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
8
by: Mark Neilson | last post by:
1. What is the best way to make a single instance of my top level class (DLL) internally available to all other members of the assembly? The top level object is where all other access is made in...
4
by: Ben R. | last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing with value versus object types. If I'm correct, when I do: dim x as integer There's no need for "new" to be called...
1
by: philwozza | last post by:
Hi I have a THREAD class that uses the static variable NextThreadID to store the id of the next thread to be created and a static Mutex to protect it. class THREAD { public: int Start(void);...
9
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
4
by: sks | last post by:
hi , i Have a code snippet as follows class ABC { int &r; ABC(int a=0): r(a) {} }; int main() {
8
by: sarathy | last post by:
Hi, I read the following points in K&R "Section A8.7 Initialization". Seems like i have a problem with them. * All expressions in the initialization of constant object/array must be constant...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
13
by: JohnQ | last post by:
Why would anyone write: class SomeThing // class littered with non-domain single-instancing code :( { private: SomeThing(); static SomeThing* pInstance_; public: static SomeThing*...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.