Espaces de noms
Variantes
Affichages
Actions

default initialization

De cppreference.com
< cpp‎ | language

 
 
Langage C++
Sujets généraux
Contrôle de flux
Instructions conditionnelles
Instructions d'itération
Instructions de saut
Fonctions
déclaration de fonction
expression lambda
fonction générique
spécificateur inline
spécification d'exception (obsolète)
spécificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spécificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littéraux
Expressions
opérateurs alternatifs
Utilitaires
Types
déclaration typedef
déclaration 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 mémoire
Classes
Qualificatifs spécifiques aux membres de classe
Fonctions membres spéciales
Modèles
classes génériques
fonctions génériques
spécialisation de modèles
paquets de paramètres (C++11)
Divers
Assembleur
 
Fournit la valeur initiale par défaut 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.

Sommaire

[modifier] Syntaxe

T object ; (1)
new T ; (2)

[modifier] Explication

Initialisation par défaut est effectuée 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 durée de stockage automatique est déclarée 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 durée de stockage dynamique est créée 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 données 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 défaut 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 défaut est appelé à fournir la valeur initiale pour le nouvel objet .
    Original:
    If T is a class type, the constructeur par défaut 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 élément du tableau est initialisé par défaut .
    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 défaut 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.

[modifier] Notes

Initialisation par défaut de non-classe des variables avec la durée de stockage automatique et dynamique produit des objets avec des valeurs indéterminées (objets statiques et fil-locale se zéro 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 zéro initialisé)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Référence ne peut pas être initialisé par défaut .
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.

[modifier] 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
}


[modifier] Voir aussi