Espaces de noms
Variantes
Actions

explicit type conversion

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
 
Convertit entre les types en utilisant une combinaison de conversions implicites et explicites .
Original:
Converts between types using a combination of explicit and implicit conversions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifier]Syntaxe

(new_type) expression (1)
new_type( expression) (2)
Renvoie une valeur de type .. new_type
Original:
Returns a value of type new_type.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifier]Explication

1)
Lorsque l'expression de cast de style C est rencontrée, le compilateur tente les expressions de transtypage suivantes, dans cet ordre:
Original:
When the C-style cast expression is encountered, the compiler attempts the following cast expressions, in this order:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
a)const_cast<new_type>(expression)
b)
static_cast<new_type>(expression), avec des extensions: pointeur ou une référence à une classe dérivée est en outre autorisé à être transtypage vers un pointeur ou une référence à la classe de base sans ambiguïté (et vice versa), même si la classe de base est inaccessible (c'est-à-cette distribution ne tient pas compte de l'héritage privé spécificateur) . La même chose s'applique à la coulée pointeur vers le membre de pointeur vers membre du sans ambiguïté non virtuelle de base
Original:
static_cast<new_type>(expression), with extensions: pointer or reference to a derived class is additionally allowed to be cast to pointer or reference to unambiguous base class (and vice versa) even if the base class is inaccessible (that is, this cast ignores the private inheritance specifier). Same applies to casting pointer to member to pointer to member of unambigous non-virtual base
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
c)
static_cast (avec extensions), suivie par const_cast
Original:
static_cast (with extensions) followed by const_cast
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
d)reinterpret_cast<new_type>(expression)
e)
reinterpret_cast suivie const_cast
Original:
reinterpret_cast followed by const_cast
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
@ @ Le premier choix qui satisfait aux exigences de l'opérateur de cast correspondant est sélectionné, même si elle ne peut pas être compilé (voir l'exemple) .
Original:
@@ The first choice that satisfies the requirements of the respective cast operator is selected, even if it cannot be compiled (see example).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
@ @ En outre, la notation coulé style C est autorisé à déposer à partir de, vers et entre les pointeurs de type de classe incomplète. Si les deux expression et new_type sont des pointeurs vers des types de classes incomplètes, il est précisé si static_cast ou reinterpret_cast obtient sélectionné .
Original:
@@ In addition, C-style cast notation is allowed to cast from, to, and between pointers to incomplete class type. If both expression and new_type are pointers to incomplete class types, it's unspecified whether static_cast or reinterpret_cast gets selected.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
La distribution fonctionnelle se compose d'un spécificateur de type simple ou un spécificateur typedef (en d'autres termes, un seul mot nom du type: unsignedint(expression) ou int*(expression) ne sont pas valides), suivie par une seule expression entre parenthèses. Cette fonte est exactement équivalente à l'expression de cast de style C correspondant .
Original:
The functional cast consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: unsignedint(expression) or int*(expression) are not valid), followed by a single expression in parentheses. This cast is exactly equivalent to the corresponding C-style cast expression.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Comme pour toutes les expressions de transtypage, le résultat est le suivant:
Original:
As with all cast expressions, the result is:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • une lvalue si new_type est un type de référence lvalue ou une référence rvalue à un type de fonction;
    Original:
    an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • un xValue si new_type est une référence rvalue de type d'objet;
    Original:
    an xvalue if new_type is an rvalue reference to object type;
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • un prvalue autrement .
    Original:
    a prvalue otherwise.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[modifier]Exemple

double f =3.14;unsignedint n1 =(unsignedint)f;// C-style castunsignedint n2 =unsigned(f);// functional cast   class C1;class C2; C2* foo(C1* p){return(C2*)p;// casts incomplete type to incomplete type}   // In this example, C-style cast is interpreted as static_cast// even though it would work as reinterpret_caststruct A {};struct I1 : A {};struct I2 : A {};struct D : I1, I2 {};   int main(){ D* d = nullptr; A* a =(A*)d;// compile-time error A* a =reinterpret_cast<A*>(d);// this compiles}


close