◐ Shell
clean mode source ↗

copy initialization — cppreference.com

De cppreference.com

<metanoindex/>

Initialise un objet d'un autre objet

Original:

Initializes an object from another object

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

Syntaxe

T object = other ; (1)
f(other); (2)
return other; (3)
catch ( T other) ; (4)
T array [ N ] = { other }; (5)

Explication

Initialisation de la copie est effectuée dans les cas suivants:

Original:

Copy initialization is performed in the following situations:

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

1)

quand une variable nommée (automatique, statique ou thread-local) est déclarée avec l'initialiseur constituée d'un signe égal suivi d'une expression .

Original:

when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of an equals sign followed by an expression.

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

2)

en passant un argument à une fonction par valeur

Original:

when passing an argument to a function by value

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

3)

au retour d'une fonction qui retourne la valeur

Original:

when returning from a function that returns by value

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

4)

lors de la capture d'une exception en valeur

Original:

when catching an exception by value

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

5)

dans le cadre de initialisation d'agrégats, pour initialiser chaque élément pour lequel il est prévu une initialisation

Original:

as part of initialisation d'agrégats, to initialize each element for which an initializer is provided

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

Les effets de l'initialisation de copie sont:

Original:

The effects of copy initialization are:

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

  • Si T est un type de classe et le type de other est cv-inconditionnel de la version T ou une classe dérivée de T, les constructeurs de T sont examinées et la meilleure correspondance est choisi par la résolution de surcharge. Le constructeur est alors appelée pour initialiser l'objet .

    Original:

    If T is a class type and the type of other is cv-unqualified version of T or a class derived from T, the constructors of T are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.

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

  • Si T est un type de classe et le type de other est différent, ou si T n'est pas de type classe, mais le type de other est un type de classe, des séquences définies par l'utilisateur de conversion qui peut convertir le type de other à T sont examinés et le meilleur on est sélectionné grâce à la résolution de surcharge. Le résultat de la conversion, qui est une prvalue temporaire du type de destination, est ensuite utilisé pour diriger-initialiser l'objet. La dernière étape est généralement éliminé et le résultat de la fonction de conversion est construit directement dans la mémoire allouée pour l'objet cible, mais le constructeur de copie est nécessaire pour être accessible même si elle n'est pas utilisée .

    Original:

    If T is a class type, and the type of other is different, or if T is non-class type, but the type of other is a class type, des séquences définies par l'utilisateur de conversion that can convert from the type of other to T are examined and the best one is selected through overload resolution. The result of the conversion, which is a prvalue temporary of the destination type, is then used to diriger-initialiser the object. The last step is usually éliminé and the result of the conversion function is constructed directly in the memory allocated for the target object, but the copy constructor is required to be accessible even though it's not used.

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

  • Dans le cas contraire (si aucun T ni le type de other sont les types de classes), conversions standard sont utilisés, si nécessaire, pour convertir la valeur de other à la version cv-inconditionnel de T .

    Original:

    Otherwise (if neither T nor the type of other are class types), conversions standard are used, if necessary, to convert the value of other to the cv-unqualified version of T.

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

Notes

Copier-initialisation est moins permissive que l'initialisation directe: la copie d'initialisation ne tient compte que non explicites constructeurs et les fonctions de conversion définis par l'utilisateur .

Original:

Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions.

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

Si other est une expression rvalue, déplacer constructeur seront sélectionnés par la résolution de surcharge et a appelé au cours de la copie d'initialisation .

Original:

If other is an rvalue expression, déplacer constructeur will be selected by overload resolution and called during copy-initialization.

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

Le signe égal, =, dans la copie d'initialisation d'une variable nommée n'est pas lié à l'opérateur d'affectation. Les surcharges d'opérateur d'affectation n'ont aucun effet sur la copie d'initialisation .

Original:

The equals sign, =, in copy-initialization of a named variable is not related to the assignment operator. Assignment operator overloads have no effect on copy-initialization.

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

Exemple

#include <string>
#include <utility>
#include <memory>

int main()
{
    std::string s = "test"; // OK: constructor is non-explicit
    std::string s2 = std::move(s); // this copy-initialization performs a move

//  std::unique_ptr<int> p = new int(1); // error: constructor is explicit
    std::unique_ptr<int> p(new int(1)); // OK: direct-initialization

    int n = 3.14;    // floating-integral conversion
    const int b = n; // const doesn't matter
    int c = b;       // ...either way
}

Voir aussi