static_cast conversion
Aus cppreference.com
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Konvertiert zwischen Typen mit einer Kombination von impliziten und benutzerdefinierte Konvertierungen .
Original:
Converts between types using a combination of implicit and user-defined conversions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Inhaltsverzeichnis |
[Bearbeiten]Syntax
static_cast < new_type> ( expression) | |||||||||
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.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten]Erklärung
Nur die folgenden Konvertierungen können mit static_cast getan werden, außer wenn solche Umwandlungen würde werfen Konstantheit oder Volatilität .
Original:
Only the following conversions can be done with static_cast, except when such conversions would cast away constness or volatility.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
Wenn ein temporäres Objekt vom Typ new_type deklariert und initialisiert werden mit expression, wie new_type Temp(expression);, die implizite Konvertierungen ein Aufruf an den Konstruktor new_type oder einen Anruf an eine benutzerdefinierte Konvertierung Betreiber beinhalten kann, dann static_cast<type>(expression) berechnet und gibt den Wert dieser temporären Objekt .
Original:
If a temporary object of type new_type can be declared and initialized with expression, as by new_type Temp(expression);, which may involve implicit conversions, a call to the constructor of new_type or a call to a user-defined conversion operator, then static_cast<type>(expression) computes and returns the value of that temporary object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
Wenn new_type ein Zeiger oder Referenz zu einem gewissen Klasse
D
ist und die Art der expression ist ein Zeiger oder Verweis auf seine nicht-virtuelle Basis B
, static_cast
führt eine niedergeschlagenen. Solche static_cast
macht keine Runtime-Kontrollen, um sicherzustellen, dass das Objekt Laufzeittyp eigentlich D
und können nur sicher verwendet werden, wenn diese Voraussetzung durch andere Mittel, wie z. B. bei der Umsetzung statische Polymorphismus gewährleistet ist. Sichere niedergeschlagenen kann mit dynamic_cast getan werden .Original:
If new_type is a pointer or reference to some class
D
and the type of expression is a pointer or reference to its non-virtual base B
, static_cast
performs a downcast. Such static_cast
makes no runtime checks to ensure that the object's runtime type is actually D
, and may only be used safely if this precondition is guaranteed by other means, such as when implementing statische Polymorphismus. Safe downcast may be done with dynamic_cast.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
Wenn new_type ein rvalue Referenztyp ist, wandelt
static_cast
den Wert expression zu xWert. Diese Art von static_cast
wird verwendet, um Bewegung Semantik in std::move umzusetzen. (seit C++11)Original:
If new_type is an rvalue reference type,
static_cast
converts the value of expression to xvalue. This type of static_cast
is used to implement move semantics in std::move. (seit C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
4)
Wenn new_type die Art void (möglicherweise cv-qualifiziert) ist,
static_cast
verwirft den Wert expression nach Auswertung it .Original:
If new_type is the type void (possibly cv-qualified),
static_cast
discards the value of expression after evaluating it.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
5)new_type der Art der expression existiert, das heißt nicht beinhalten Lvalue-zu-R-Wert, Array-zu-Zeiger, Funktions-zu-Pointer, Null-Zeiger, null Elementzeiger oder boolesche Umwandlung, dann
static_cast
ausführen können, Die Umkehrung dieser implizite Konvertierung .Original:
If an
implizite Konvertierung</div> sequence from new_type to the type of expression exists, that does not include lvalue-to-rvalue, array-to-pointer, function-to-pointer, null pointer, null member pointer, or boolean conversion, then
Original:
implicit conversion
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
static_cast
can perform the inverse of that implicit conversion.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
6)
Wenn Umwandlung von expression um new_type beinhaltet lvalue-to-rvalue, array-to-pointer oder Funktion-to-Zeiger-Konvertierung können explizit durch
static_cast
durchgeführt werden .Original:
If conversion of expression to new_type involves lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion, it can be performed explicitly by
static_cast
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
7)
Scoped Aufzählungstyp kann ein Integer-oder Floating-Point-Typ konvertiert werden. (seit C++11)
Original:
Scoped enumeration type can be converted to an integer or floating-point type. (seit C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
8)
Integer, Fließkomma-oder Aufzählungstyp kann auf einen beliebigen Aufzählungstyp (das Ergebnis ist nicht spezifiziert, wenn der Wert expression, umgerechnet auf der Enumeration zugrunde liegenden Typ, gehört nicht zu den Ziel-Enumerationswerte) umgewandelt werden
Original:
Integer, floating-point, or enumeration type can be converted to any enumeration type (the result is unspecified if the value of expression, converted to the enumeration's underlying type, is not one of the target enumeration values)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
9)
Ein Zeiger auf Mitglied einer Klasse
D
kann upcast zu einem Zeiger auf ein Element der Basisklasse B
. Diese static_cast
macht keine Kontrollen, um sicherzustellen, das Mitglied tatsächlich existiert in der Laufzeittyp der spitzen-to object .Original:
A pointer to member of some class
D
can be upcast to a pointer to member of its base class B
. This static_cast
makes no checks to ensure the member actually exists in the runtime type of the pointed-to object.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
10)
A prvalue vom Typ Zeiger auf void (möglicherweise cv-qualifiziert) können Zeiger auf jede Art umgewandelt werden. Umwandlung von einem Zeiger auf Zeiger auf void und zurück zum Zeiger auf das ursprüngliche (oder mehr cv-qualifiziert) Art bewahrt seinen ursprünglichen Wert .
Original:
A prvalue of type pointer to void (possibly cv-qualified) can be converted to pointer to any type. Conversion of any pointer to pointer to void and back to pointer to the original (or more cv-qualified) type preserves its original value.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
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.
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]Notes
static_cast kann auch zur Funktion Überlastungen durch Ausführen einer Funktion zu Zeiger Umstellung auf spezifische Art, wie in std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper)); disambiguate werden
Original:
static_cast may also be used to disambiguate function overloads by performing a function-to-pointer conversion to specific type, as in std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper));
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten]Keywords
[Bearbeiten]Beispiel
#include <vector>#include <iostream> struct B {};struct D : B {}; enumclass E { ONE, TWO, THREE };enum EU { ONE, TWO, THREE }; int main(){// 1: initializing conversionint n =static_cast<int>(3.14);std::cout<<"n = "<< n <<'\n';std::vector<int> v =static_cast<std::vector<int>>(10);std::cout<<"v.size() = "<< v.size()<<'\n'; // 2: static downcast D d; B& br = d;// upcast via implicit conversion D& another_d =static_cast<D&>(br);// downcast // 3: lvalue to xvaluestd::vector<int> v2 =static_cast<std::vector<int>&&>(v);std::cout<<"after move, v.size() = "<< v.size()<<'\n'; // 4: discarded-value expressionstatic_cast<void>(v2.size()); // 5. inverse of implicit conversion// todo // 6. array-to-pointer followed by upcast D a[10]; B* dp =static_cast<B*>(a); // 7. scoped enum to int or float E e = E::ONE;int one =static_cast<int>(e); // 8. int to enum, enum to another enum E e2 =static_cast<E>(one); EU eu =static_cast<EU>(e2); // 9. pointer to member upcast// todo // 10. void* to any typevoid* voidp =&e;std::vector<int>* p =static_cast<std::vector<int>*>(voidp);}
Output:
n = 3 v.size() = 10 after move, v.size() = 0