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. |
Un constructeur par défaut est un constructeur qui peut être appelé sans arguments (soit défini avec une liste de paramètres vide, ou avec des arguments fournis par défaut pour chaque paramètre). Un type avec un constructeur public par défaut 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.
Sommaire |
[modifier]Syntaxe
ClassName ( ) ; | (1) | ||||||||
ClassName :: ClassName ( ) body | (2) | ||||||||
ClassName() = delete ; | (3) | (depuis C++11) | |||||||
ClassName() = default ; | (4) | (depuis C++11) | |||||||
[modifier]Explication
1)
Déclaration d'un constructeur par défaut
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)
Définition 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 génération automatique d'un constructeur par défaut
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 génération automatique d'un constructeur par défaut
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 défaut sont appelées 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.
- créer des objets ou des tableaux de statique, la durée de stockage local des threads, et automatique qui sont déclarées sans un initialiseur, (T obj; ou T arr[10];)Original:creating objects or arrays of static, thread-local, and automatic storage duration that are declared without an initializer, (T obj; or T 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 création d'objets d'une durée 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 création de tableaux de durée de stockage dynamique avec le new T[n] expressionOriginal:creating arrays of dynamic storage duration with the expression new T[n]The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - créer de la valeur des objets temporaires initialisés avec l'expression fonte T() .Original:creating value-initialized temporary objects with the cast expression T().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 par défaut
Si aucun constructeur définies par l'utilisateur de toute nature sont prévus pour un type de classe (struct, class ou union), le compilateur va toujours déclarer un constructeur par défaut en tant que membre
inline public
de sa catégorie. Si certains constructeurs définis par l'utilisateur sont présents, l'utilisateur peut toujours forcer la génération du constructeur implicitement déclarée 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.
[modifier]Supprimé constructeur par défaut implicite-déclarée
Le constructeur par défaut implicite déclarées ou défaut de
T
classe est (avant C++11) indéfini / défini 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.
T
a un membre de type de référence (sans support-or-equal initializer (depuis C++11)) .Original:T
has 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.T
a un membre const (sans initializer (depuis C++11) accolades ou égal) ou défini par l'utilisateur constructeur par défaut .Original:T
has a const member (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.T
comporte un élément (sans initializer (depuis C++11) accolades ou égal), qui possède un constructeur par défaut supprimé, ou son constructeur par défaut est ambigu ou inaccessibles à partir de ce constructeur .Original:T
has 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.T
a une base directe ou virtuelle qui possède un constructeur par défaut supprimé, ou qu'il est ambigu ou inaccessibles à partir de ce constructeur .Original:T
has 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.T
a une base directe ou virtuelle qui a un destructeur supprimé, ou un destructeur qui est inaccessible à partir de ce constructeur .Original:T
has 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.T
est un union avec au moins un membre variante avec constructor (depuis C++11) défaut non négligeable .Original:T
is a union with 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.T
est un union et tous ses membres sont des variantes const .Original:T
is a union and all of its variant members are const.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Trivial constructeur par défaut
Le constructeur par défaut implicite-déclarée 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.
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.T
n'a pas de membres non statiques avec initialiseurs brace-ou-égal (depuis C++11)Original:T
has 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
T
possède un constructeur par défaut trivialOriginal:Every direct base ofT
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. - Chaque membre non-statique de type classe a un constructeur par défaut 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 défaut trivial est un constructeur qui n'effectue aucune action. Les objets avec des constructeurs par défaut triviaux peuvent être créés en utilisant reinterpret_cast sur tout système de stockage, par exemple, sur la mémoire allouée avec std::malloc. Tous les types de données compatibles avec le langage C (types POD) sont trivialement par défaut 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.
[modifier]Implicitement défini par le constructeur par défaut
Si le constructeur par défaut implicite-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, et il a exactement le même effet que le constructeur défini par l'utilisateur avec un corps vide et vide liste d'initialisation. Autrement dit, il appelle les constructeurs par défaut 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.
[modifier]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 memberconstint c;// const member// Bad::Bad() is deleted}; int main(){ A a; B b;// D d; // compile error E e;// F f; // compile error}