Namensräume
Varianten
Aktionen

explicit type conversion

Aus cppreference.com
< cpp‎ | language

 
 
Sprache C + +
Allgemeine Themen
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Flusskontrolle
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Bedingte Ausführung Aussagen
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterationsanweisungen
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Gehe Aussagen
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktionen
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktion Erklärung
Lambda-Funktion Erklärung
Funktions-Template
inline-Spezifizierer
Exception-Spezifikationen (veraltet)
noexcept Spezifizierer (C++11)
Ausnahmen
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Namespaces
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv Planer
Lagerdauer Planer
constexpr Spezifizierer (C++11)
auto Spezifizierer (C++11)
alignas Spezifizierer (C++11)
Initialisierung
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Literale
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expressions
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
alternative Darstellungen
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Typ Aliasdeklaration (C++11)
Attribute (C++11)
Wirft
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
impliziten Konvertierungen
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-Stil und funktionale Besetzung
Speicherzuweisung
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Class-spezifische Funktion Eigenschaften
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
explizit (C++11)
statisch
Besondere Member-Funktionen
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Templates
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Klassen-Template
Funktions-Template
Template-Spezialisierung
Parameter Packs (C++11)
Verschiedenes
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Inline Montage
 
Konvertiert zwischen Typen mit einer Kombination aus expliziten und impliziten Konvertierungen .
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.

[Bearbeiten] Syntax

( new_type ) expression (1)
new_type ( expression ) (2)
Gibt einen Wert vom Typ 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.

[Bearbeiten] Erklärung

1)
Wenn die C-Casts Ausdruck gefunden wird, versucht der Compiler die folgenden Besetzung Ausdrücke, in dieser Reihenfolge:
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), mit Erweiterungen: Zeiger oder eine Referenz auf eine abgeleitete Klasse ist zusätzlich erlaubt, den Zeiger oder eine Referenz auf die eindeutige Basisklasse (und umgekehrt) gegossen werden, auch wenn die Basisklasse nicht zugänglich ist (das heißt, diese Stimmen ignoriert die private Vererbung specifier) . Gleiches gilt für Gießen Zeiger auf ein Element, um Zeiger auf ein Element der unambigous nicht-virtuelle Basis
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 (mit Erweiterungen) durch const_cast gefolgt
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 gefolgt von 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.
@ @ Die erste Wahl, die den Anforderungen des jeweiligen Cast-Operator erfüllt ausgewählt ist, auch wenn es nicht kompiliert werden (siehe Beispiel) .
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.
@ @ Darüber hinaus C-style Umwandlungsnotation darf von, nach und zwischen Zeiger auf unvollständigen Klasse Typ umwandeln. Wenn beide expression und new_type sind Zeiger auf unvollständigen Klassentypen, es ist nicht angegeben, ob static_cast oder reinterpret_cast gewählt wird .
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)
Die funktionale Besetzung besteht aus einem einfachen Typspezifizierer oder eine typedef Spezifizierer (in anderen Worten, aus einem einzigen Wort Typname: unsigned int(expression) oder int*(expression) sind nicht gültig), gefolgt von einem einzigen Ausdruck in Klammern. Diese Besetzung entspricht exakt der entsprechenden C-Casts Ausdruck .
Original:
The functional cast consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: unsigned int(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.
Wie bei allen Besetzung Ausdrücke, ist das Ergebnis:
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.
  • ein L-Wert, wenn new_type ist ein L-Wert Referenz-Typ oder ein rvalue Bezug auf Art funktionieren;
    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.
  • ein xWert wenn new_type ist ein rvalue Verweis auf Objekttyp;
    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.
  • a prvalue sonst .
    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.

[Bearbeiten] Beispiel

double f = 3.14;
unsigned int n1 = (unsigned int)f; // C-style cast
unsigned int 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_cast
struct 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
}