cpp/language/constructor

Constructor is a special non-static 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. (Not to be confused with std.)

Syntax
Constructors are declared using member of the following form:

Where 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 of a constructor declaration are, , and  (in particular, no return type is allowed). Note that 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 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 , each of which has the following syntax:

@1@ Initializes the base or member named by using  or, if  is empty, @2@ Initializes the base or member named by using  (which becomes  if the list is empty and  when initializing an aggregate) @3@ Initializes multiple bases using a

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

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. The member initializer list is the place where non-default initialization of these objects can be specified. For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified. No initialization is performed for or  that do not have a member initializer.

The initializers where names a  are ignored during construction of any class that is not the most derived class of the object that's being constructed.

Names that appear in or  are evaluated in scope of the constructor:

Exceptions that are thrown from member initializers may be handled by

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 direct bases are initialized at that point), 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 is the static type of the class that's being constructed (dynamic dispatch does not propagate down the inheritance hierarchy) and virtual calls (but not static calls) to  member functions are undefined behavior.

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

Note: same applies to.

{{rrev|since=c++11|

Delegating constructor
If the name of the class itself appears as in the member initializer list, then the list must consist of that one member initializer only; such a 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. }}

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 bases 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 bases are initialized in left-to-right order as they appear in this class's base-specifier list. @3@ Then, non-static data member 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 wouldn't be able to ensure that the order of destruction is the reverse of the order of construction.)