Namespace
Varianti

dynamic_cast conversion

Da cppreference.com.
< cpp‎ | language

 
 
Linguaggio C + +
Temi generali
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.
Controllo del flusso
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.
Dichiarazioni esecuzione condizionale
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.
Iterazione dichiarazioni
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.
Vai dichiarazioni
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.
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dichiarazione di funzione
lambda funzione dichiarazione
funzione di modello
specificatore inline
eccezioni specifiche (deprecato)
noexcept specificatore (C++11)
Eccezioni
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Spazi dei nomi
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
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 specificatori
Durata di stoccaggio specificatori
constexpr specificatore (C++11)
specificatore auto (C++11)
alignas specificatore (C++11)
Inizializzazione
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Letterali
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Espressioni
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rappresentazioni alternative
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.
Tipi
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
Tipo alias dichiarazione (C++11)
attributi (C++11)
Lancia
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversioni implicite
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Fusione C-stile e funzionale
Occupazione della memoria
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.
Classi
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Specifiche per una classe di funzioni proprietà
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.
esplicito (C++11)
statico
Funzioni membro speciali
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.
Modelli
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
classe template
funzione di modello
modello di specializzazione
parametri confezioni (C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaggio in linea
 
Converte in modo sicuro puntatori e riferimenti alle classi alto, in basso, e lateralmente lungo la gerarchia di ereditarietà.
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.

Indice

[modifica] Sintassi

dynamic_cast < new_type > ( expression )
Restituisce un valore di tipo new_type o genera un'eccezione.
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.

[modifica] Spiegazione

Solo le seguenti conversioni può essere fatto con dynamic_cast, tranne quando tali conversioni avrebbe gettato via constness''' o volatilità.
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)
Se il tipo di expression è esattamente new_type o una versione meno cv-qualificata new_type, il risultato è 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)
Se il valore del expression è il valore di puntatore nullo, il risultato è il valore del puntatore nullo new_type tipo
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)
Se new_type è un puntatore o riferimento a Base, e il tipo di expression è un puntatore o riferimento a Derived, dove Base è un unico, classe di base accessibile Derived, il risultato è un puntatore o riferimento alla classe subobject Base all'interno Derived oggetto appuntito o identificato da expression. (Nota: cast implicito e static_cast in grado di eseguire questa conversione, come pure)
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)
Se expression è un puntatore o riferimento a un tipo polimorfico, e new_type è un puntatore void, il risultato è un puntatore all'oggetto più derivata rilevato o riferimento 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)
Se expression è un puntatore o un riferimento a un tipo di Base polimorfico, e new_type è un puntatore o un riferimento al tipo Derived di run-time controllo viene effettuato:
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)
L'oggetto più indicato derivato / identificato da expression viene esaminata. Se, in tale oggetto, punti expression / ispira ad una base pubblica di Derived, e se un solo oggetto secondario del tipo Derived deriva dal subobject rilevato / identificato da expression, allora il risultato dei punti di fusione / si riferisce a quella subobject Derived. (Questo è noto come il "downcast")
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)
Altrimenti, se i punti expression / si riferisce ad una base pubblica dell'oggetto più derivata, e, simultanously, l'oggetto più derivata ha una classe univoca base pubblica di Derived tipo, il risultato dei punti di fusione / riferisce a quella Derived (questo è noto come "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)
In caso contrario, il controllo di runtime non riesce. Se il dynamic_cast viene usato su puntatori, il valore del puntatore nullo di new_type tipo viene restituito. Se è stato utilizzato su riferimenti, il std::bad_cast eccezione viene generata.
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)
dynamic_cast quando viene utilizzato in un costruttore o un distruttore (direttamente o indirettamente), e expression riferisce all'oggetto che è attualmente in costruzione / distruzione, l'oggetto è considerato l'oggetto più derivata. Se new_type non è un puntatore o un riferimento alla costruzione della propria classe / distruttore o una delle sue basi, il comportamento non è definito.
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.
Simile alle espressioni cast, il risultato è:
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.
  • un lvalue se new_type è un tipo di riferimento lvalue (expression deve essere un valore assegnabile)
    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.
  • un xValue new_type se è un tipo di riferimento rvalue (expression può essere lvalue o rvalue)
    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.
  • un prvalue altrimenti (in questo caso, se new_type è un tipo di puntatore)
    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.

[modifica] Nota

Downcast può essere eseguita anche con static_cast, che evita il costo del controllo di esecuzione, ma è sicuro solo se il programma in grado di garantire, attraverso una logica altra, che l'oggetto puntato da expression è sicuramente Derived.
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.

[modifica] Parole chiave

dynamic_cast

[modifica] Esempio

#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

[modifica] Vedi anche