In what order are non-static data members initialized?
Asked Answered
L

2

53

In the following code, when the ctor of X is called will the ctor of A or B be called first? Does the order in which they are placed in the body of the class control this? If somebody can provide a snippet of text from the C++ standard that talks about this issue, that would be perfect.

class A {};
class B {};
class X
{
 A a;
 B b;
};
Lorenz answered 19/4, 2010 at 18:35 Comment(0)
L
86

The order is the order they appear in the class definition - this is from section 12.6.2 of the C++ Standard:

5 Initialization shall proceed in the following order:

— First, and only for the constructor of the most derived class as described below, virtual base classes shall be initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base class names in the derived class base-specifier-list.

— Then, direct base classes shall be initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).

— Then, nonstatic data members shall be initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).

— Finally, the body of the constructor is executed. [Note: the declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. ]

Liturgy answered 19/4, 2010 at 18:41 Comment(1)
Where does member data list initiation fall?Systematism
R
15

Initialization is always in the order that the class members appear in your class definition, so in your example a, then b.

There is a sequence point between the initialization of each member and you can pass a reference to a yet-to-be initialized member into the constructor of a class member but you would only be able to use it in limited ways (such as taking its address to form a pointer), other uses may well cause undefined behaviour.

Destruction of class members always happens in the reverse order of construction.

Order of initialization of bases and members is defined in 12.6.2 [class.base.init]/5.

Rosel answered 19/4, 2010 at 18:37 Comment(6)
Could you provide a reference?Lorenz
To add to this, if you have a constructor for X like this - X(): b(), a() {} a is still initialized first.Gujral
I know this. I am asking for a reference. I confidently said this to someone who asked for a reference and I could not find one. Hence the question.Lorenz
@Nikhil: I was just checking the para number. Now added.Rosel
@Nikhil: If you know this then why ask the question? Why not just ask for a reference instead of asking about something that you already know as well?Rosel
@Nikhil: I don't think anybody would expect you to remember reference locations into the standard. Just know where to look it up; you're unlikely to need to know the exact reference often.Rosel

© 2022 - 2024 — McMap. All rights reserved.