Order of construction

L

Lilith

Does the standard define in what order components of a class are
instantiated? I created a class called Grid that has a M x N array.
Each element of this 2D array is a structure that has it's own
constructor. The Grid constructor does its own initialization of the
members of the 2D array but apparently the constructor for the
elements of array comes in after this and changes that to whatever the
constructor for the struct says it should be.

I would imagine that in order for the Grid constructor to be able to
act on the 2D array the 2D array itself would have had to have been
instantiated before the Grid constructor kicked in.

Any insight on whether there's a mandated order for these things?
 
H

hurcan solter

Does the standard define in what order components of a class are
instantiated? I created a class called Grid that has a M x N array.
Each element of this 2D array is a structure that has it's own
constructor. The Grid constructor does its own initialization of the
members of the 2D array but apparently the constructor for the
elements of array comes in after this and changes that to whatever the
constructor for the struct says it should be.

I would imagine that in order for the Grid constructor to be able to
act on the 2D array the 2D array itself would have had to have been
instantiated before the Grid constructor kicked in.i

Any insight on whether there's a mandated order for these things?

I have hard time imagining same object constructed twice.probably you
think you are constructing the elements but they are already
constructed and what you do is noop
Cant really say without seeing some code of course.

yes there is an order, base classes constructed in order as listed in
the class declaration,and members in order as they are declared which
can be overriden in member initializer list
 
K

Kai-Uwe Bux

Lilith said:
Does the standard define in what order components of a class are
instantiated?

You mean initialized?

Nonstatic class members are initialized in order of declaration. See
[12.6.2/5] for details.

I created a class called Grid that has a M x N array.
Each element of this 2D array is a structure that has it's own
constructor. The Grid constructor does its own initialization of the
members of the 2D array but apparently the constructor for the
elements of array comes in after this and changes that to whatever the
constructor for the struct says it should be.

Huh? This is incomprehensible without seeing the code.
I would imagine that in order for the Grid constructor to be able to
act on the 2D array the 2D array itself would have had to have been
instantiated before the Grid constructor kicked in.

Any insight on whether there's a mandated order for these things?

Initialization order is defined according to [12.6.2/5]. What happens in
your case is impossible to say without seeing code.


Best

Kai-Uwe Bux
 
M

Martin Vuille

s.com:
yes there is an order, base classes constructed in order as
listed in the class declaration,and members in order as they
are declared which can be overriden in member initializer
list

Not sure whether I am reading this right. But the order in which
members are specified in the initializer list is irrelevant: the
members are always constructed in the same order as that in which
they were declared.

MV
 
K

Kai-Uwe Bux

Martin said:
s.com:


Not sure whether I am reading this right. But the order in which
members are specified in the initializer list is irrelevant: the
members are always constructed in the same order as that in which
they were declared.

Correct. The order of construction is independent of initializer lists. This
way, members are guaranteed to be constructed in the same order in all
constructors so that the compiler can easily ensure that the destructor
destroys them in reverse order.


Best

Kai-Uwe Bux
 
Z

Zeppe

Kai-Uwe Bux said:
Correct. The order of construction is independent of initializer lists. This
way, members are guaranteed to be constructed in the same order in all
constructors so that the compiler can easily ensure that the destructor
destroys them in reverse order.

and actually gcc for example, to avoid confusion, emits a warning in
case the order in the initialiser list is different from the one in
which elements are declared in the class.

Regards,

Zeppe
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top