Copy constructors
De cppreference.com
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Un constructeur de copie de
T
classe est un constructeur non-template dont le premier paramètre est T&, const T&, volatile T& ou constvolatile T&, et soit il n'y a pas d'autres paramètres, ou dans le reste des paramètres de l'ensemble des valeurs par défaut. Un type avec un constructeur de copie publique est CopyConstructible
.Original:
A copy constructor of class
T
is a non-template constructor whose first parameter is T&, const T&, volatile T&, or constvolatile T&, and either there are no other parameters, or the rest of the parameters all have default values. A type with a public copy constructor is CopyConstructible
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Sommaire |
[modifier]Syntaxe
class_name ( constclass_name& ) | (1) | ||||||||
class_name ( constclass_name& ) = default; | (1) | ||||||||
class_name ( constclass_name& ) = delete; | (1) | ||||||||
[modifier]Explication
# Déclaration typique d'un constructeur de copie
Original:
# Typical declaration of a copy constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
# Forcer un constructeur de copie qui seront générés par le compilateur
Original:
# Forcing a copy constructor to be generated by the compiler
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
# Éviter constructeur par défaut implicite
Original:
# Avoiding implicit default constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Le constructeur de copie est appelé chaque fois qu'un objet est initialisé à partir d'un autre objet du même type, qui comprend
Original:
The copy constructor is called whenever an object is initialized from another object of the same type, which includes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- l'initialisation, ou T a = b;T a(b);, où b est de
T
typeOriginal:initialization, T a = b; or T a(b);, where b is of typeT
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - passage argument de fonction: f(a);, où
a
est de typeT
etf
est void f(T t)Original:function argument passing: f(a);, wherea
is of typeT
andf
is void f(T t)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - retour de la fonction: return a; l'intérieur d'une fonction telle que T f(), où
a
estT
type, qui n'a pas de constructeur mouvement .Original:function return: return a; inside a function such as T f(), wherea
is of typeT
, which has no move constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Implicitement déclarée constructeur de copie
Si aucun définis par l'utilisateur constructeurs de copie sont prévus pour un type de classe (struct, class ou union), le compilateur va toujours déclarer un constructeur de copie en tant que membre
inline public
de sa catégorie. Ce constructeur de copie implicitement déclaré a la forme T::T(const T&)
si toutes les conditions suivantes sont réunies:Original:
If no user-defined copy constructors are provided for a class type (struct, class, or union), the compiler will always declare a copy constructor as an
inline public
member of its class. This implicitly-declared copy constructor has the form T::T(const T&)
if all of the following is true:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- toutes les bases directes et virtuelles de
T
avoir un constructeur de copie avec des références à la const const volatile ou que leurs premiers paramètresOriginal:all direct and virtual bases ofT
have copy constructors with references to const or to const volatile as their first parametersThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - tous les membres non statiques de
T
avoir un constructeur de copie avec des références à la const const volatile ou que leurs premiers paramètresOriginal:all non-static members ofT
have copy constructors with references to const or to const volatile as their first parametersThe 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, le constructeur de copie implicitement déclaré est T::T(T&). (Notez qu'en raison de ces règles, le constructeur de copie implicitement déclarée ne peut pas se lier à un argument volatile lvalue)
Original:
Otherwise, the implicitly-declared copy constructor is T::T(T&). (Note that due to these rules, the implicitly-declared copy constructor cannot bind to a volatile lvalue argument)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Une classe peut avoir plusieurs constructeurs de copie, par exemple à la fois T::T(const T&) et T::T(T&). Si certains constructeurs de copie définis par l'utilisateur sont présents, l'utilisateur peut toujours forcer la génération du constructeur par recopie implicitement déclarée avec le mot-clé
default
(depuis C++11) .Original:
A class can have multiple copy constructors, e.g. both T::T(const T&) and T::T(T&). If some user-defined copy constructors are present, the user may still force the generation of the implicitly declared copy constructor with the keyword
default
(depuis C++11).The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Supprimé implicitement déclarée constructeur de copie
Le constructeur de copie implicitement déclarée ou défaut de
T
classe est (avant C++11) indéfini / défini comme' (depuis C++11) supprimé dans l'une des conditions suivantes est remplie:Original:
The implicitly-declared or defaulted copy constructor for class
T
is undefined (avant C++11) / defined as deleted (depuis C++11) in any of the following is true:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
T
a des membres de données non statiques qui ne peuvent pas être copiés (avez supprimé, inaccessible, ou un constructeur de copie ambigus)Original:T
has non-static data members that cannot be copied (have deleted, inaccessible, or ambiguous copy constructors)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
a de la classe de base directe ou virtuelle qui ne peuvent pas être copiés (a supprimé, un constructeur de copie inaccessibles, ou ambigus)Original:T
has direct or virtual base class that cannot be copied (has deleted, inaccessible, or ambiguous copy constructors)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
a de la classe de base directe ou virtuelle avec un destructeur supprimé ou inaccessibleOriginal:T
has direct or virtual base class with a deleted or inaccessible destructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
a un constructeur défini par l'utilisateur déménagement ou (depuis C++11) opérateur d'affectation mouvementOriginal:T
has a user-defined move constructor or move assignment operator (depuis C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
est une union et comporte un élément variante avec des non-trivial (depuis C++11) constructeur de copieOriginal:T
is a union and has a variant member with non-trivial copy constructor (depuis C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
a un membre de données de type de référence rvalue (depuis C++11)Original:T
has a data member of rvalue reference type (depuis C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Constructeur de copie trivial
Le constructeur de copie implicitement déclaré pour
T
classe est triviale si l'ensemble des conditions suivantes est remplie:Original:
The implicitly-declared copy constructor for class
T
is trivial if all of the following is true:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
T
n'a pas de fonctions membres virtuellesOriginal:T
has no virtual member functionsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
a pas de classes de base virtuellesOriginal:T
has no virtual base classesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.- Le constructeur de copie sélectionné pour chaque base directe de
T
est trivialOriginal:The copy constructor selected for every direct base ofT
is trivialThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Le constructeur de copie sélectionné pour chaque type de classe non statique (ou un tableau de type de classe) memeber de
T
est trivialOriginal:The copy constructor selected for every non-static class type (or array of class type) memeber ofT
is trivialThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Un constructeur de copie trivial est un constructeur qui crée une copie par octet de la représentation de l'objet de l'argument, et n'effectue aucune autre action. Les objets avec des constructeurs de copie triviales peuvent être copiées en copiant leurs représentations d'objets manuellement, par exemple, avec std::memmove. Tous les types de données compatibles avec le langage C (types POD) sont trivialement copiable .
Original:
A trivial copy constructor is a constructor that creates a bytewise copy of the object representation of the argument, and performs no other action. Objects with trivial copy constructors can be copied by copying their object representations manually, e.g. with std::memmove. All data types compatible with the C language (POD types) are trivially copyable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Implicitement définie par le constructeur de copie
Si le constructeur de copie implicitement déclarée n'est pas supprimé ou trivial, il est défini (c'est-à-corps d'une fonction est généré et compilé) par le compilateur. Pour les types de union, le constructeur de copie de définition implicite copie la représentation de l'objet (comme par std::memmove). Pour les types de classes non syndiqués (class et struct), le constructeur effectue membre à part entière-sage copie des bases de l'objet et non-membres statiques, dans leur ordre d'initialisation, en utilisant l'initialisation directe .
Original:
If the implicitly-declared copy constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. For union types, the implicitly-defined copy constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the constructor performs full member-wise copy of the object's bases and non-static members, in their initialization order, using direct initialization.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
La génération du constructeur par recopie de définition implicite est deprecated (depuis C++11) si
T
a un destructeur défini par l'utilisateur ou définie par l'utilisateur opérateur d'affectation copie .Original:
The generation of the implicitly-defined copy constructor is deprecated (depuis C++11) if
T
has a user-defined destructor or user-defined copy assignment operator.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Notes
Dans de nombreuses situations, un constructeur de copie sont optimisés supprimée, même si elles produisent des effets secondaires observables, élision copie voir
Original:
In many situations, copy constructors are optimized out even if they would produce observable side-effects, see élision copie
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Exemple
struct A {int n; A(int n=1): n(n){} A(const A& a): n(a.n){}// user-defined copy ctor}; struct B : A {// implicit default ctor B::B()// implicit copy ctor B::B(const B&) }; struct C : B { C(): B(){}private: C(const C&);// non-copiable, C++98 style}; int main(){ A a1(7); A a2(a1);// calls the copy ctor B b; B b2 = b; A a3 = b;// conversion to A& and copy ctorvolatile A va(10);// A a4 = va; // compile error C c;// C c2 = c; // compile error}