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

default initialization cppreference.com
cppreference.com
Espaces de noms
Variantes

default initialization

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
 
Fournit la valeur initiale par dfaut pour un nouvel objet .
Original:
Provides the default initial value to a new 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 ; (1)
new T ; (2)

Explication

Initialisation par dfaut est effectue dans trois situations:
Original:
Default initialization is performed in three 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 avec la dure de stockage automatique est dclare sans initialisation
Original:
when a variable with automatic storage duration is declared with no initializer
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
quand un objet avec la dure de stockage dynamique est cre par une nouvelle expression sans un initialiseur
Original:
when an object with dynamic storage duration is created by a new-expression without an initializer
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
quand une classe de base ou un membre non statique de donnes n'est pas mentionn dans un liste d'initialisation constructeur et que le constructeur est appel .
Original:
when a base class or a non-static data member is not mentioned in a constructor liste d'initialisation and that constructor is called.
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 par dfaut sont les suivants:
Original:
The effects of default 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, le constructeur par dfaut est appel fournir la valeur initiale pour le nouvel objet .
    Original:
    If T is a class type, the constructeur par dfaut is called to provide the initial value for the new 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 tableau, chaque lment du tableau est initialis par dfaut .
    Original:
    If T is an array type, every element of the array is default-initialized.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Sinon, rien n'est fait .
    Original:
    Otherwise, nothing is done.
    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 const-qualifi, il doit tre d'un type classe avec un constructeur par dfaut fourni par l'utilisateur .
Original:
If T is a const-qualified type, it must be a class type with a user-provided default constructor.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Notes

Initialisation par dfaut de non-classe des variables avec la dure de stockage automatique et dynamique produit des objets avec des valeurs indtermines (objets statiques et fil-locale se zro initialis)
Original:
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-locale objects get zro initialis)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Rfrence ne peut pas tre initialis par dfaut .
Original:
Reference cannot be default-initialized.
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>
struct T1 {};
class T2 { 
    int mem;
 public:
    T2() {} // "mem" not in initializer list
};
int n; // This is not default-initialization, the value is zero.
int main()
{
    int n;            // non-class: the value is undeterminate
    std::string s;    // calls default ctor, the value is "" (empty string)
    std::string a[2]; // calls default ctor, creates two empty strings
//    int& r;         // error: default-initializing a reference
//    const int n;    // error: const non-class type
//    const T1 nd;    // error: const class type with implicit ctor
    T1 t1; // ok, calls implicit default ctor
    const T2 t2; // ok, calls the user-provided default ctor 
                 // t2.mem is default-initialized
}


Voir aussi


Web Proxy Viewer  |  New URL  |  Original Page