Namensräume
Varianten
Aktionen

dynamic_cast 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
 
Sicher wandelt Zeiger und Referenzen auf Klassen nach oben, unten und seitlich entlang der Vererbungshierarchie .
Original:
Safely converts pointers and references to classes up, down, and sideways along the inheritance hierarchy.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Inhaltsverzeichnis

[Bearbeiten] Syntax

dynamic_cast < new_type > ( expression )
Gibt einen Wert vom Typ new_type oder eine Ausnahme auslöst .
Original:
Returns a value of type new_type or throws an exception.
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

Nur die folgenden Konvertierungen können mit dynamic_cast getan werden, außer wenn solche Umwandlungen würde werfen Konstantheit oder Volatilität .
Original:
Only the following conversions can be done with dynamic_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.
1)
Wenn der Typ der expression ist die genau new_type oder ein weniger cv-qualifizierten Version des new_type, ist das Ergebnis expression .
Original:
If the type of expression is the exactly new_type or a less cv-qualified version of new_type, the result is expression.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Wenn der Wert expression ist die Null-Zeiger-Wert, ist das Ergebnis NULL-Zeiger-Wert des Typs new_type
Original:
If the value of expression is the null pointer value, the result is the null pointer 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.
3)
Wenn ein Zeiger oder new_type Bezugnahme auf Base ist, und die Art der expression ist ein Zeiger oder Verweis auf Derived, wo Base ist eine einzigartige, zugänglich Basisklasse Derived, ist das Ergebnis ein Zeiger oder ein Verweis auf die Klasse Base Subobjekt innerhalb des Derived Objekt spitz oder identifiziert durch expression. (Anmerkung: impliziten Besetzung und static_cast kann diese Umwandlung so gut funktioniert)
Original:
If new_type is a pointer or reference to Base, and the type of expression is a pointer or reference to Derived, where Base is a unique, accessible base class of Derived, the result is a pointer or reference to the Base class subobject within the Derived object pointed or identified by expression. (note: implicit cast and static_cast can perform this conversion as well)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
Wenn expression ein Zeiger oder Referenz auf einen polymorphen Typ ist, und new_type ist ein Zeiger auf void, ist das Ergebnis ein Zeiger auf den meisten abgeleitete Objekt spitz oder referenziert von expression .
Original:
If expression is a pointer or reference to a polymorphic type, and new_type is a pointer to void, the result is a pointer to the most derived object pointed or referenced by expression.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
Wenn expression ein Zeiger oder Referenz auf einen polymorphen Typ Base ist und new_type ist ein Zeiger oder ein Verweis auf die Art Derived a run-time wird überprüft:
Original:
If expression is a pointer or reference to a polymorphic type Base, and new_type is a pointer or reference to the type Derived a run-time check is performed:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
a)
Die abgeleitete Objekt / identifiziert durch expression hingewiesen wird untersucht. Wenn in diesem Objekt, expression Punkte / referes zu einem öffentlichen Basis Derived, und wenn nur ein Unterobjekt Derived Typs aus dem Unterobjekt abgeleitet ist spitz / durch expression identifiziert, dann wird das Ergebnis der gegossenen Punkte / bezieht sich auf diejenige Derived Subobjekt. (Dies wird als "niedergeschlagenen" bekannt)
Original:
The most derived object pointed/identified by expression is examined. If, in that object, expression points/referes to a public base of Derived, and if only one subobject of Derived type is derived from the subobject pointed/identified by expression, then the result of the cast points/refers to that Derived subobject. (this is known as the "downcast")
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
b)
Andernfalls, wenn expression Punkte / bezieht sich auf eine öffentliche Basis der meisten abgeleitete Objekt, und, simultan, hat das abgeleitete Objekt eine eindeutige öffentliche Basisklasse vom Typ Derived, das Ergebnis der gegossenen Punkte / bezieht sich auf diejenige Derived (dies ist bekannt als "sidecast")
Original:
Otherwise, if expression points/refers to a public base of the most derived object, and, simultanously, the most derived object has an unambiguous public base class of type Derived, the result of the cast points/refers to that Derived (this is known as the "sidecast")
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
c)
Andernfalls schlägt der Runtime-Check. Wenn der Zeiger auf dynamic_cast verwendet wird, wird die Null-Zeiger-Wert des Typs new_type zurückgegeben. Wenn es auf Referenzen verwendet wurde, wird die Ausnahme std::bad_cast geworfen .
Original:
Otherwise, the runtime check fails. If the dynamic_cast is used on pointers, the null pointer value of type new_type is returned. If it was used on references, the exception std::bad_cast is thrown.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
6)
Wenn dynamic_cast in einem Konstruktor oder Destruktor (direkt oder indirekt) verwendet wird, und expression verweist auf das Objekt, die momentan im Bau / Zerstörung, wird das Objekt als die meisten abgeleiteten Objekt sein. Wenn new_type nicht ein Zeiger oder Referenz für den Bau der / Destruktor eigenen Klasse oder eine ihrer Basen, ist das Verhalten undefiniert .
Original:
When dynamic_cast is used in a constructor or a destructor (directly or indirectly), and expression refers to the object that's currently under construction/destruction, the object is considered to be the most derived object. If new_type is not a pointer or reference to the construction's/destructor's own class or one of its bases, the behavior is undefined.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Ähnlich wie bei anderen Besetzung Ausdrücke, ist das Ergebnis:
Original:
Similar to other 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 lvalue Referenztyp (expression muss ein L-Wert sein)
    Original:
    an lvalue if new_type is an lvalue reference type (expression must be an lvalue)
    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 Referenztyp (expression kann lvalue oder rvalue sein)
    Original:
    an xvalue if new_type is an rvalue reference type (expression may be lvalue or rvalue)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • ein prvalue anderenfalls (in diesem Fall, wenn ein Zeiger new_type Typ ist)
    Original:
    a prvalue otherwise (in this case, if new_type is a pointer type)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Hinweis

Niedergeschlagen kann auch mit static_cast, die die Kosten der Überprüfung zur Laufzeit vermieden durchgeführt werden, aber es ist nur sicher, wenn das Programm zu gewährleisten, durch eine andere Logik, dass das Objekt durch expression ist definitiv Derived hingewiesen .
Original:
Downcast can also be performed with static_cast, which avoids the cost of the runtime check, but it's only safe if the program can guarantee, through some other logic, that the object pointed to by expression is definitely Derived.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Keywords

dynamic_cast

[Bearbeiten] Beispiel

#include <iostream>
 
struct V {
    virtual void f() {};  // must be polymorphic to use runtime-checked dynamic_cast
};
struct A : virtual V {};
struct B : virtual V {
  B(V* v, A* a) {
    // casts during construction
    dynamic_cast<B*>(v); // well-defined: v of type V*, V base of B, results in B*
    dynamic_cast<B*>(a); // undefined behavior: a has type A*, A not a base of B
  }
};
struct D : A, B {
    D() : B((A*)this, this) { }
};
 
struct Base {
    virtual ~Base() {}
};
 
struct Derived: Base {
    virtual void name() {}
};
 
int main()
{
    D d; // the most derived object
    A& a = d; // upcast, dynamic_cast may be used, but unnecessary
    D& new_d = dynamic_cast<D&>(a); // downcast
    B& new_b = dynamic_cast<B&>(a); // sidecast
 
 
    Base* b1 = new Base;
    if(Derived* d = dynamic_cast<Derived*>(b1))
    {
        std::cout << "downcast from b1 to d successful\n";
        d->name(); // safe to call
    }
 
    Base* b2 = new Derived;
    if(Derived* d = dynamic_cast<Derived*>(b2))
    {
        std::cout << "downcast from b2 to d successful\n";
        d->name(); // safe to call
    }
 
    delete b1;
    delete b2;
}

Output:

downcast from b2 to d successful

[Bearbeiten] Siehe auch