[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/language/default_constructor [Back]  [Original]

Default constructors cppreference.com
cppreference.com
Espaces de noms
Variantes

Default constructors

De cppreference.com

<metanoindex/>

 
 
Langage C++
Sujets gnraux
Contrle de flux
Instructions conditionnelles
Instructions d'itration
Instructions de saut
Fonctions
dclaration de fonction
expression lambda
fonction gnrique
spcificateur inline
spcification d'exception (obsolte)
spcificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spcificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littraux
Expressions
oprateurs alternatifs
Utilitaires
Types
dclaration typedef
dclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mmoire
Classes
Qualificatifs spcifiques aux membres de classe
Fonctions membres spciales
Modles
classes gnriques
fonctions gnriques
spcialisation de modles
paquets de paramtres (C++11)
Divers
Assembleur
 
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.

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.
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.
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.
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.
ClassName est l'identificateur de la classe de fermeture
Original:
ClassName is the identifier of the enclosing class
The text has been machine-translated via Google Translate.
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.
  • 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; 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 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] expression
    Original:
    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.
  • crer de la valeur des objets temporaires initialiss 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.

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.

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.
  • T a un membre de type de rfrence (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 dfini par l'utilisateur constructeur par dfaut .
    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 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:
    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 possde un constructeur par dfaut 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) dfaut non ngligeable .
    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.

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.
  • T n'a pas de fonctions membres virtuelles
    Original:
    T has no virtual member functions
    The 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 virtuelles
    Original:
    T has no virtual base classes
    The 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 possde un constructeur par dfaut trivial
    Original:
    Every direct base of T has a trivial default constructor
    The 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 trivial
    Original:
    Every non-static member of class type has a trivial default constructor
    The 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.

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.

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
}

Web Proxy Viewer  |  New URL  |  Original Page