◐ Shell
clean mode source ↗

Initializer list — cppreference.com

De cppreference.com

<metanoindex/>

(Ne pas confondre avec std :: initializer_list)

Original:

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Ils sont la partie d'un constructeur qui est responsable de membre et l'initialisation ancêtre

Original:

They are the part of a constructor which is responsible for member and ancestor initialization

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Syntaxe

constructor_signature : member_constructor_calls { constructor_body }

Explication

La liste d'initialisation est le lieu où l'initialisation de l'objet doit se produire, c'est là que les constructeurs des classes de base et les membres sont appelés .

Original:

The initializer list is the place where initialization of the object should occur, there is where the constructors for base classes and members are called.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Les membres sont initialisées dans l'ordre où elles sont déclarées, et non tels qu'ils apparaissent dans la liste d'initialisation .

Original:

Members are initialized in the same order as they are declared, not as they appear in the initializer list.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Si un paramètre dans le constructeur a le même nom que l'un des membres, l'ambiguïté de cet identificateur étant passé dans un appel de constructeur à l'intérieur de la liste intializer est résolu choisir le paramètre (et non le membre) .

Original:

If a parameter in the constructor has the same name as one of the members, the ambiguity of that identifier being passed in a constructor call inside the intializer list is resolved choosing the parameter (and not the member).

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Membres ou des classes de base ne présentent pas dans la liste par défaut sera construite

Original:

Members or base classes not present in the list will be default constructed

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Exemple

struct Class : public Base
{
    int x;
    int 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 constructor body

    Class ( double a )
      : y ( a+1 ),
        x ( y ) // x will be initialized before y, this means that its value here is undefined
    {}          // No base class constructor in list, this is the same as calling Base()

    Class()
    try
      : Base ( 789 ),
        x ( 0 ),
        y ( 0 )
    {
        // no exception
    }
    catch (...)
    {
        // exception occurred on initialization
    }
};