When a constructor has a member initialization list, the initializations take place


Initialization lists are an alternative technique for initializing an object's data members in a constructor. For instance, the constructor

date::date(int m, int d, int y)
{
    month = m;
    day = d;
    year = y;
}

can instead be written using an initialization list:

date::date(int m, int d, int y) : month(m), day(d), year(y)
{
}

Both built-in types and objects can be initialized using initialization lists. Arrays (including C strings) can not be initialized using initialization lists.

Initializing an object this way results in a constructor call. For example, if a class called employee has a date object called hire_date as one of its data members, we could use an initializer list to initialize the hire_date data member in the employee constructor:

employee::employee(const string& name, int hire_month, int hire_day, int hire_year) :
                   employee_name(name), hire_date(hire_month, hire_day, hire_year)
{
}

Initialization Lists and Scope Issues

If you have a data member of your class that has the same name as a parameter to your constructor, then the initialization list "does the right thing." For instance,

date::date(int month, int day, int year) : month(month), day(day), year(year)
{
}

is roughly equivalent to

date::date(int month, int day, int year)
{
    this->month = month;
    this->day = day;
    this->year = year;
}

That is, the compiler knows which month belongs to the object, and which month is the local variable that is declared in the member function.

When to Use Initializer Lists

Using initialization lists to initialize data members in a constructor can be convenient if you don't need to do any error-checking on the constructor arguments. There are also several instances in C++ where the use of an initializer list to initialize a data member is actually required:

  1. Data members that are const but not static must be initialized using an initialization list.
  2. Data members that are references must be initialized using an initialization list.
  3. An initialization list can be used to explicitly call a constructor that takes arguments for a data member that is an object of another class (see the employee constructor example above).
  4. In a derived class constructor, an initialization list can be used to explicitly call a base class constructor that takes arguments.

Constructor is a special non-static member function of a class that is used to initialize objects of its class type.

In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual base subobjects and non-static data members. ( Not to be confused with std::initializer_list )

Contents

  • 1 Syntax
  • 2 Explanation
    • 2.1 Delegating constructor
    • 2.2 Inheriting constructors
  • 3 Initialization order
  • 4 Example
    • 4.1 Defect reports
    • 4.2 References

[edit] Syntax

Constructors are declared using member function declarators of the following form:

class-name ( parameter-list(optional) ) except-spec(optional) attr(optional) (1)

Where class-name must name the current class (or current instantiation of a class template), or, when declared at namespace scope or in a friend declaration, it must be a qualified class name.

The only specifiers allowed in the decl-specifier-seq of a constructor declaration are friend, inline, explicit and constexpr (in particular, no return type is allowed). Note that cv- and ref-qualifiers are not allowed either; const and volatile semantics of an object under construction don't kick in until the most-derived constructor completes.

The body of a function definition of any constructor, before the opening brace of the compound statement, may include the member initializer list, whose syntax is the colon character :, followed by the comma-separated list of one or more member-initializers, each of which has the following syntax

class-or-identifier ( expression-list(optional) ) (1)
class-or-identifier brace-init-list (2) (since C++11)
parameter-pack ... (3) (since C++11)
class-or-identifier - any identifier, class name, or decltype expression that names a non-static data member, a direct or virtual base, or (for delegating constructors) the class itself
expression-list - possibly empty, comma-separated list of the parameters to pass to the constructor of the base or member
brace-init-list - brace-enclosed list of comma-separated initializers and nested braced-init-lists
parameter-pack - name of a variadic template parameter pack

struct S {
    int n;
    S(int); // constructor declaration
    S() : n(7) {} // constructor definition.
                  // ": n(7)" is the initializer list
                  // ": n(7) {}" is the function body
};
S::S(int x) : n{x} {} // constructor definition. ": n{x}" is the initializer list
int main()
{
    S s; // calls S::S()
    S s2(10); // calls S::S(int)
}

[edit] Explanation

Constructors have no names and cannot be called directly. They are invoked when initialization takes place, and they are selected according to the rules of initialization. The constructors without explicit specifier are converting constructors. The constructors with a constexpr specifier make their type a LiteralType. Constructors that may be called without any argument are default constructors. Constructors that take another object of the same type as the argument are copy constructors and move constructors.

Before the compound statement that forms the function body of the constructor begins executing, initialization of all direct bases, virtual bases, and non-static data members is finished. Member initializer list is the place where non-default initialization of these objects can be specified. For members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.

The initializers where class-or-identifier names a virtual base class are ignored during execution of constructors of any class that is not the most derived class of the object that's being constructed.

Names that appear in expression-list or brace-init-list are evaluated in scope of the constructor:

class X {
    int a, b, i, j;
public:
    const int& r;
    X(int i)
      : r(a) // initializes X::r to refer to X::a
      , b{i} // initializes X::b to the value of the parameter i
      , i(i) // initializes X::i to the value of the parameter i
      , j(this->i) // initializes X::j to the value of X::i
    { }
};

Exceptions that are thrown from member initializers may be handled by function-try-block

Member functions (including virtual member functions) can be called from member initializers, but the behavior is undefined if not all direct bases are initialized at that point.

For virtual calls (if the bases are initialized), the same rules apply as the rules for the virtual calls from constructors and destructors: virtual member functions behave as if the dynamic type of *this is the class that's being constructed (dynamic dispatch does not propagate down the inheritance hierarchy) and virtual calls (but not static calls) to pure virtual member functions are undefined behavior.

If a non-static data member has an in-class brace-or-equal initializer and also appears in a member initializer list, then member initializer list is executed and brace-or-equal initializer is ignored:

struct S {
    int n = 42;
    S() : n(7) {} // will set n to 7, not 42
};

(since C++11)

Reference members cannot be bound to temporaries in a member initializer list:

struct A {
    A() : v(42) { }  // Error
    const int& v;
};

Note: same applies to in-class brace-or-equal initializer

(since C++14)

Delegating constructor

If the name of the class itself appears as class-or-identifier in the member initializer list, then the list must consist of that one member initializer only; such constructor is known as the delegating constructor, and the constructor selected by the only member of the initializer list is the target constructor

In this case, the target constructor is selected by overload resolution and executed first, then the control returns to the delegating constructor and its body is executed.

Delegating constructors cannot be recursive.

Inheriting constructors

See using declaration.

(since C++11)

[edit] Initialization order

The order of member initializers in the list is irrelevant: the actual order of initialization is as follows:

1) If the constructor is for the most-derived class, virtual base classes are initialized in the order in which they appear in depth-first left-to-right traversal of the base class declarations (left-to-right refers to the appearance in base-specifier lists)

2) Then, direct base classes are initialized in left-to-right order as they appear in this class's base-specifier list

3) Then, non-static data members are initialized in order of declaration in the class definition.

4) Finally, the body of the constructor is executed

(Note: if initialization order was controlled by the appearance in the member initializer lists of different constructors, then the destructor wouldn't be able to ensure that the order of destruction is the reverse of the order of construction)

[edit] Example

struct Class : public Base
{
    unsigned char x;
    unsigned char y;
 
    Class ( int x )
      : Base ( 123 ), // initialize base class
        x ( x ),      // x (member) is initialized with x (parameter)
        y { 0 }       // y initialized to 0
    {}                // empty compound statement
 
    Class ( double a )
      : y ( a+1 ),
        x ( y ) // x will be initialized before y, its value here is indeterminate
    {} // base class constructor does not appear in the list, it is
       // default-initialized (not the same as if Base() were used, which is value-init)
 
    Class()
    try // function-try block begins before the function body, which includes init list
      : Base ( 789 ),
        x ( 0 ),
        y ( 0 )
    {
        // ...
    }
    catch (...)
    {
        // exception occurred on initialization
    }
};

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1696 C++14 reference members could be initialized to temporaries (whose lifetime would end at the end of ctor) such init is ill-formed

[edit] References

  • C++11 standard (ISO/IEC 14882:2011):
  • 12.1 Constructors [class.ctor]
  • 12.6.2 Initializing bases and members [class.base.init]

  • C++98 standard (ISO/IEC 14882:1998):
  • 12.1 Constructors [class.ctor]
  • 12.6.2 Initializing bases and members [class.base.init]

When using initializer lists in what order are member variables initialized?

Const member variables must be initialized. A member initialization list can also be used to initialize members that are classes. When variable b is constructed, the B(int) constructor is called with value 5. Before the body of the constructor executes, m_a is initialized, calling the A(int) constructor with value 4.

What is constructor initialization list?

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.

What is a constructor initializer list and what are its uses?

Constructor is a special non-static member function of a class that is used to initialize objects of its class type. In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual bases and non-static data members.

Should my constructors use initialization lists or assignment?

Conclusion: All other things being equal, your code will run faster if you use initialization lists rather than assignment.