Default 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. |
<metanoindex/>
Un constructeur par dfaut est un constructeur qui peut tre appel sans arguments (soit dfini avec une liste de paramtres vide, ou avec des arguments fournis par dfaut pour chaque paramtre). Un type avec un constructeur public par dfaut est
DefaultConstructible .Original:
A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is
DefaultConstructible.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.
Syntaxe
ClassName ( ) ;
|
(1) | ||||||||
ClassName :: ClassName ( ) body
|
(2) | ||||||||
ClassName() = delete ;
|
(3) | (depuis C++11) | |||||||
ClassName() = default ;
|
(4) | (depuis C++11) | |||||||
Explication
1)
Dclaration d'un constructeur par dfaut
Original:
Declaration of a 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.
2)
Dfinition du constructeur en dehors du corps de la classe
Original:
Definition of the constructor outside the class body
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.
3)
L'inhibition de la gnration automatique d'un constructeur par dfaut
Original:
Inhibiting the automatic generation of a 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.
4)
Explicitement forcer la gnration automatique d'un constructeur par dfaut
Original:
Explicitly forcing the automatic generation of a 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.
ClassName est l'identificateur de la classe de fermetureOriginal:
ClassName is the identifier of the enclosing classThe 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.
Les constructeurs par dfaut sont appeles lorsque:
Original:
The default constructors are called when:
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.
- crer des objets ou des tableaux de statique, la dure de stockage local des threads, et automatique qui sont dclares sans un initialiseur, (
T obj;ouT arr[10];)Original:creating objects or arrays of static, thread-local, and automatic storage duration that are declared without an initializer, (T obj;orT arr[10];)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - la cration d'objets d'une dure de stockage dynamique lorsque l'expression nouvelle est crite sans un initialiseur (
new T;)Original:creating objects of dynamic storage duration when the new-expression is written without an initializer (new T;)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - la cration de tableaux de dure de stockage dynamique avec le
new T[n]expressionOriginal:creating arrays of dynamic storage duration with the expressionnew T[n]The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - crer de la valeur des objets temporaires initialiss avec l'expression fonte
T().Original:creating value-initialized temporary objects with the cast expressionT().The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Implicitement dclare constructeur par dfaut
Si aucun constructeur dfinies par l'utilisateur de toute nature sont prvus pour un type de classe (
struct, class ou union), le compilateur va toujours dclarer un constructeur par dfaut en tant que membre inline public de sa catgorie. Si certains constructeurs dfinis par l'utilisateur sont prsents, l'utilisateur peut toujours forcer la gnration du constructeur implicitement dclare avec le mot-cl default (depuis C++11) .Original:
If no user-defined constructors of any kind are provided for a class type (
struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class. If some user-defined constructors are present, the user may still force the generation of the implicitly declared 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.
Supprim constructeur par dfaut implicite-dclare
Le constructeur par dfaut implicite dclares ou dfaut de
T classe est (avant C++11) indfini / dfini comme' (depuis C++11) supprim si l'une des conditions suivantes est remplie:Original:
The implicitly-declared or defaulted default constructor for class
T is undefined (avant C++11) / defined as deleted (depuis C++11) if 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.
Ta un membre de type de rfrence (sans support-or-equal initializer (depuis C++11)) .Original:Thas a member of reference type (without a brace-or-equal initializer (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.Ta un membreconst(sans initializer (depuis C++11) accolades ou gal) ou dfini par l'utilisateur constructeur par dfaut .Original:Thas aconstmember (without a brace-or-equal initializer (depuis C++11)) or a user-defined default constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.Tcomporte un lment (sans initializer (depuis C++11) accolades ou gal), qui possde un constructeur par dfaut supprim, ou son constructeur par dfaut est ambigu ou inaccessibles partir de ce constructeur .Original:Thas a member (without a brace-or-equal initializer (depuis C++11)), which has a deleted default constructor, or its default constructor is ambiguous or inaccessible from this constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.Ta une base directe ou virtuelle qui possde un constructeur par dfaut supprim, ou qu'il est ambigu ou inaccessibles partir de ce constructeur .Original:Thas a direct or virtual base which has a deleted default constructor, or it is ambiguous or inaccessible from this constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.Ta une base directe ou virtuelle qui a un destructeur supprim, ou un destructeur qui est inaccessible partir de ce constructeur .Original:Thas a direct or virtual base which has a deleted destructor, or a destructor that is inaccessible from this constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.Test ununionavec au moins un membre variante avec constructor (depuis C++11) dfaut non ngligeable .Original:Tis aunionwith at least one variant member with non-trivial default 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.Test ununionet tous ses membres sont des variantesconst.Original:Tis aunionand all of its variant members areconst.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Trivial constructeur par dfaut
Le constructeur par dfaut implicite-dclare pour
T classe est triviale si l'ensemble des conditions suivantes est remplie:Original:
The implicitly-declared default 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.
Tn'a pas de fonctions membres virtuellesOriginal:Thas 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.Ta pas de classes de base virtuellesOriginal:Thas 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.Tn'a pas de membres non statiques avec initialiseurs brace-ou-gal (depuis C++11)Original:Thas no non-static members with brace-or-equal initializers (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.- Chaque base directe de
Tpossde un constructeur par dfaut trivialOriginal:Every direct base ofThas a trivial default constructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Chaque membre non-statique de type classe a un constructeur par dfaut trivialOriginal:Every non-static member of class type has a trivial default constructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Un constructeur par dfaut trivial est un constructeur qui n'effectue aucune action. Les objets avec des constructeurs par dfaut triviaux peuvent tre crs en utilisant reinterpret_cast sur tout systme de stockage, par exemple, sur la mmoire alloue avec std::malloc. Tous les types de donnes compatibles avec le langage C (types POD) sont trivialement par dfaut constructible .
Original:
A trivial default constructor is a constructor that performs no action. Objects with trivial default constructors can be created by using reinterpret_cast on any storage, e.g. on memory allocated with std::malloc. All data types compatible with the C language (POD types) are trivially default-constructible.
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.
Implicitement dfini par le constructeur par dfaut
Si le constructeur par dfaut implicite-dclare n'est pas supprim ou trivial, il est dfini (c'est--corps d'une fonction est gnr et compil) par le compilateur, et il a exactement le mme effet que le constructeur dfini par l'utilisateur avec un corps vide et vide liste d'initialisation. Autrement dit, il appelle les constructeurs par dfaut des bases et des membres non statiques de cette classe .
Original:
If the implicitly-declared default constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler, and it has exactly the same effect as a user-defined constructor with empty body and empty initializer list. That is, it calls the default constructors of the bases and of the non-static members of this class.
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.
Exemple
struct A {
int x;
A(int x = 1) : x(x) {} // user-defined default ctor
};
struct B : A {
// B::B() is implicitly-defined, calls A::A()
};
struct C {
A obj;
// C::C() is implicitly-defined, calls A::A()
};
struct D : A {
D(int y) : A(y) {}
// D::D() is not declared because another constructor exists
};
struct E : A
{
E(int y) : A(y) {}
E() = default; // explicitly defaulted, calls A::A()
};
struct F {
int& ref; // reference member
const int c; // const member
// Bad::Bad() is deleted
};
int main()
{
A a;
B b;
// D d; // compile error
E e;
// F f; // compile error
}