dynamic_cast (C++ Type Cast Operator)
Go Up to Keywords, Alphabetical Listing Index
In the expression:
dynamic_cast< T > (ptr)
T must be a pointer or a reference to a defined class type or void*. The argument ptr must be an expression that resolves to a pointer or reference.
If T is void*, then ptr must also be a pointer. In this case, the resulting pointer can access any element of the class that is the most derived element in the hierarchy. Such a class cannot be a base for any other class.
Conversions from a derived class to a base class, or from one derived class to another, are as follows: if T is a pointer and ptr is a pointer to a non-base class that is an element of a class hierarchy, the result is a pointer to the unique subclass. References are treated similarly. If T is a reference and ptr is a reference to a non-base class, the result is a reference to the unique subclass.
A conversion from a base class to a derived class can be performed only if the base is a polymorphic type.
The conversion to a base class is resolved at compile time. A conversion from a base class to a derived class, or a conversion across a hierarchy is resolved at run time.
If successful, dynamic_cast< T > (ptr) converts ptr to the desired type. If a pointer cast fails, the returned pointer is valued 0. If a cast to a reference type fails, the Bad_cast exception is thrown.
- Const cast(typecast Operator)
- Reinterpret cast(typecast Operator)
- Static cast(typecast Operator)
- __rtti, -RT Option