Constructors and member initializer lists

Constructors are non-static member functions declared with a special declarator syntax, they are used to initialize objects of their class types.

# Notes

Feature-test macro Value Std Feature __cpp_delegating_constructors 200604L (C++11) Delegating constructors

# Example

#include <fstream>
#include <string>
#include <mutex>
 
struct Base
{
    int n;
};   
 
struct Class : public Base
{
    unsigned char x;
    unsigned char y;
    std::mutex m;
    std::lock_guard<std::mutex> lg;
    std::fstream f;
    std::string s;
 
    Class(int x) : Base{123}, // initialize base class
        x(x),     // x (member) is initialized with x (parameter)
        y{0},     // y initialized to 0
        f{"test.cc", std::ios::app}, // this takes place after m and lg are initialized
        s(__func__), // __func__ is available because init-list is a part of constructor
        lg(m),    // lg uses m, which is already initialized
        m{}       // m is initialized before lg even though it appears last here
    {}            // empty compound statement
 
    Class(double a) : y(a + 1),
        x(y), // x will be initialized before y, its value here is indeterminate
        lg(m)
    {} // base class initializer 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
      : Class(0.0) // delegate constructor
    {
        // ...
    }
    catch (...)
    {
        // exception occurred on initialization
    }
};
 
int main()
{
    Class c;
    Class c1(1);
    Class c2(0.1);
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 194C++98the declarator syntax of constructor only allowedat most one function specifier (e.g. a constructorcannot be declared inline explicit)multiple functionspecifiers allowed
CWG 257C++98it was unspecified whether an abstract class shouldprovide member initializers for its virtual base classesspecified as not requiredand such member initializersare ignored during execution
CWG 263C++98the declarator syntax of constructorprohibited constructors from being friendsallowed constructorsto be friends
CWG 1345C++98anonymous union members without defaultmember initializers were default-initializedthey are not initialized
CWG 1435C++98the meaning of “class name” in thedeclarator syntax of constructor was unclearchanged the syntax to a specializedfunction declarator syntax
CWG 1696C++98reference members could be initialized to temporaries(whose lifetime would end at the end of constructor)such initializationis ill-formed

# See also