Namespaces
Variants
Views
Actions

dynamic_cast conversion

From cppreference.com
< cpp‎ | language
Revision as of 07:36, 26 December 2012 by Cubbi (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Safely converts pointers and references to classes up, down, and sideways along the inheritance hierarchy.

Contents

Syntax

dynamic_cast < Template:sparam > ( Template:sparam )

Returns a value of type new_type or throws an exception.

Explanation

Only the following conversions can be done with dynamic_cast, except when such conversions would cast away constness or volatility.

1) If the type of Template:sparam is the exactly Template:sparam or a less cv-qualified version of Template:sparam, the result is Template:sparam.
2) If the value of Template:sparam is the null pointer value, the result is the null pointer value of type Template:sparam
3) If Template:sparam is a pointer or reference to Base, and the type of Template:sparam 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 Template:sparam. (note: implicit cast and static_cast can perform this conversion as well)
4) If Template:sparam 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 Template:sparam.
5) If Template:sparam 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:
a) The most derived object pointed/identified by Template:sparam is examined. If, in that object, Template:sparam points/referes to a public base of Derived, and if only one subobject of Derived type is derived from the subobject pointed/identified by Template:sparam, then the result of the cast points/refers to that Derived subobject. (this is known as the "downcast")
b) Otherwise, if Template:sparam 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")
c) Otherwise, the runtime check fails. If the dynamic_cast is used on pointers, the null pointer value of type Template:sparam is returned. If it was used on references, the exception std::bad_cast is thrown.
6) When dynamic_cast is used in a constructor or a destructor (directly or indirectly), and Template:sparam refers to the object that's currently under construction/destruction, the object is considered to be the most derived object. If Template:sparam is not a pointer or reference to the construction's/destructor's own class or one of its bases, the behavior is undefined.

Similar to other cast expressions, the result is:

Note

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 Template:sparam is definitely Derived.

Keywords

dynamic_cast

Example

#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

See also