473,403 Members | 2,270 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,403 software developers and data experts.

The order of member initialization

The oder of member initialization is the order in which the members are
defined.

So the following code is problematic:

class X{
int i;
int j;
public:
X(int val):j(val),i(j){}
};

But, can I use the following code?

class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}

};

Oct 2 '06 #1
11 2389
Tip: Please give yourself a real name or online handle. Our patience to
answer your questions is proportional to our observations of your patiences
asking them! And when asking us to recite C++ tutorials to you, you should
indicate what you are reading, and where it has failed you.

asdf wrote:
The oder of member initialization is the order in which the members are
defined.

So the following code is problematic:

class X{
int i;
int j;
public:
X(int val):j(val),i(j){}
};
It is indeed undefined behavior, from the moment that j's unassigned value
is accessed.
But, can I use the following code?

class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}

};
Yes. And now it's bad style, because ... constructor notation is better. It
clearly tells both the compiler and your colleagues what you intend. Both of
them can optimize based on this information.

Now read the /Effective C++/ books, for the category of details you
currently ask about.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 2 '06 #2
Thanks.

So you mean, the following code is correct although it is a bad style,
right?

class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}
};

Phlip wrote:
Tip: Please give yourself a real name or online handle. Our patience to
answer your questions is proportional to our observations of your patiences
asking them! And when asking us to recite C++ tutorials to you, you should
indicate what you are reading, and where it has failed you.

asdf wrote:
The oder of member initialization is the order in which the members are
defined.

So the following code is problematic:

class X{
int i;
int j;
public:
X(int val):j(val),i(j){}
};

It is indeed undefined behavior, from the moment that j's unassigned value
is accessed.
But, can I use the following code?

class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}

};

Yes. And now it's bad style, because ... constructor notation is better. It
clearly tells both the compiler and your colleagues what you intend. Both of
them can optimize based on this information.

Now read the /Effective C++/ books, for the category of details you
currently ask about.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 2 '06 #3
[Also, please do not top-post. Note I take a little time to edit your text,
so that I produce a complete post that stands-alone. (Not everyone uses
Google Groups!)]

asdf wrote:
So you mean, the following code is correct although it is a bad style,
right?

class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}
};
Yes. And you will never find a reason to do it.

One reason it's bad is because when you refactor this code (look up
"refactor"), its meaning can change unexpectedly. Same as your other
example, which was also illegal.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 2 '06 #4

"asdf" <li*********@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
The oder of member initialization is the order in which the members are
defined.

So the following code is problematic:

class X{
int i;
int j;
public:
X(int val):j(val),i(j){}
};
Undefined behavior. Why would you want to do it
that way anyway?
>
But, can I use the following code?

class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}

};
You can, but note that that is not initialization,
it's assignment. Initialize like this:

X(int val) : i(val), j(val) {}

or
X(int val) : j(val), i(val) {}

-Mike
Oct 2 '06 #5
Please don't top post, its for your benefit.
>
Phlip wrote:
Tip: Please give yourself a real name or online handle. Our patience to
answer your questions is proportional to our observations of your patiences
asking them! And when asking us to recite C++ tutorials to you, you should
indicate what you are reading, and where it has failed you.

asdf wrote:
The oder of member initialization is the order in which the members are
defined.
>
So the following code is problematic:
>
class X{
int i;
int j;
public:
X(int val):j(val),i(j){}
};
It is indeed undefined behavior, from the moment that j's unassigned value
is accessed.
But, can I use the following code?
>
class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}
>
};
Yes. And now it's bad style, because ... constructor notation is better. It
clearly tells both the compiler and your colleagues what you intend. Both of
them can optimize based on this information.

Now read the /Effective C++/ books, for the category of details you
currently ask about.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
asdf wrote:
Thanks.

So you mean, the following code is correct although it is a bad style,
right?

class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}
};
Its bad style since X::X(int val) : i(val), j(val) { } doesn't involve
assignments nor additional, waisted default construction.

If you program needs to allocate an instance of X, don't you think it
would be more efficient it it initializes the first component and then
the second without having to jump around back and forth in the memory
space? What if you had 10000 of these to instantiate in a sequenced
container? Your platform would be able to initialize all 20000 integers
in one continuous simplified flow.

Lets take the case where i and j are *not* to be assigned the same
values. Its obvious that if i is dependant on j then...
class X
{
int j;
int i; // dependant
public:
X( int n ) : j( n * 10 ), i( j ) { }
};

Oct 2 '06 #6
asdf wrote:
Thanks.

So you mean, the following code is correct although it is a bad
style,
right?

class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}
};
In this case it is correct, but a bit confusing.

In other cases, where the members are not ints, but const or reference
types, it will not work.
Bo Persson
Oct 2 '06 #7
asdf posted:
The oder of member initialization is the order in which the members are
defined.

So the following code is problematic:

class X{
int i;
int j;
public:
X(int val):j(val),i(j){}
};

g++ issues a warning for this.

But, can I use the following code?

class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}

};

Why do think you wouldn't be able to?

(1) Control enters the body of the constructor of X.
(2) (At this point, the values of i and j are indeterminant).
(3) The value of "val" is assigned to j.
(4) The value of "j" is assigned to "i".

--

Frederick Gotham
Oct 2 '06 #8
S S

Frederick Gotham wrote:
asdf posted:
The oder of member initialization is the order in which the members are
defined.

So the following code is problematic:

class X{
int i;
int j;
public:
X(int val):j(val),i(j){}
};


g++ issues a warning for this.

But, can I use the following code?

class X{
int i;
int j;
public:
X(int val)
{j=val; i=j;}

};


Why do think you wouldn't be able to?

(1) Control enters the body of the constructor of X.
(2) (At this point, the values of i and j are indeterminant).
(3) The value of "val" is assigned to j.
(4) The value of "j" is assigned to "i".

--

Frederick Gotham
In case of member initialization via initialization list, you
initialize them in order which they are declared. Even if you do not
give any initialization list (but you preferred assignment), in this
case also all members will be initialized default in order which they
are declared but after then you can modify the values as per your
assignment. And during assignment order does not matter.
Am I right Frederick?

Oct 3 '06 #9
S S posted:
In case of member initialization via initialization list, you
initialize them in order which they are declared.

Yes. The order in which the appear in the initialisation list is of no
consequence.

Even if you do not give any initialization list (but you preferred
assignment), in this case also all members will be initialized default
in order which they are declared but after then you can modify the
values as per your assignment.

No, they are not initialised at all! Take the following code for instance:

int main()
{
int i;
}

The variable, "i", is _never_ initialised.

And now take the following example:

class MyClass {
int i;

public:

MyClass()
{
i = 5;
}
};

The member variable, "i", is _never_ initialised, not even default
initialised. If you would like to default-initialise it, you would have to
change the constructor's definition to:

MyClass() : i()
And during assignment order does not matter.

Once you're past the initialisation list and into the body of the
constructor, it's a whole different ball game. You're into the land of
normal code, where sequence points and so forth play a part.

Ask more questions if you'd like further clarification.

--

Frederick Gotham
Oct 3 '06 #10
S S

Frederick Gotham wrote:
S S posted:
In case of member initialization via initialization list, you
initialize them in order which they are declared.


Yes. The order in which the appear in the initialisation list is of no
consequence.

Even if you do not give any initialization list (but you preferred
assignment), in this case also all members will be initialized default
in order which they are declared but after then you can modify the
values as per your assignment.


No, they are not initialised at all! Take the following code for instance:

int main()
{
int i;
}

The variable, "i", is _never_ initialised.

And now take the following example:

class MyClass {
int i;

public:

MyClass()
{
i = 5;
}
};

The member variable, "i", is _never_ initialised, not even default
initialised. If you would like to default-initialise it, you would have to
change the constructor's definition to:

MyClass() : i()
You are right here. Actually I should have said that for built in
types, there is no gurantee that they will be initialized before they
are getting assigned. But for all others types my statement holds true.
>
And during assignment order does not matter.


Once you're past the initialisation list and into the body of the
constructor, it's a whole different ball game. You're into the land of
normal code, where sequence points and so forth play a part.

Ask more questions if you'd like further clarification.

--

Frederick Gotham
Oct 3 '06 #11
S S posted:
You are right here. Actually I should have said that for built in
types, there is no gurantee that they will be initialized before they
are getting assigned. But for all others types my statement holds true.

If by "all other types", you mean user-defined classes which have a non-
trivial constructor, then yes.

Here's an example of a non-built-in type which won't get any sort of
initialisation:

struct MyStruct { int i; };

int main() { MyStruct obj; }

The rule of thumb is as follows:

(1) Things don't get initialised ever... unless:

(1.a) You explicitly initialise them.
or
(1.b) They have an actual constructor written for them (known as a
"non-trivial constructor" in Standard terms.

--

Frederick Gotham
Oct 3 '06 #12

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

Similar topics

5
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
9
by: Steven T. Hatton | last post by:
The following works: template <typename T> struct ID3M{ static const T ID; }; template <typename T> const T ID3M<T>::ID = {{1,0,0},{0,1,0},{0,0,1}};
15
by: Jakob Bieling | last post by:
Hi, I am aware of the fact, that the order of construction of global objects is unspecified. But I am in a situation, where I need to guarantee, that one object is created before others (not all...
2
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
7
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case...
10
by: n.torrey.pines | last post by:
Are global variables (and const's) guaranteed to be initialized before static class members (and methods) ? const int x = 19907; int get_x() { return x; } // another compilation unit: ...
7
by: Peter | last post by:
I know the order of construction of member and base class objects. My question is the following: Is the order of evaluation of argument lists for these constructors also defined? E.g. can I...
8
by: Jess | last post by:
Hello, When I define default constructors, I tend to use constructor initializers for member data. However, I was told the order in which members are initialized is determined by the order of...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.