Dynamic
Dynamic
dynamic_cast can only be used with pointers and references to classes (or with void*). Its
purpose is to ensure that the result of the type conversion points to a valid complete object of the
destination pointer type.
This naturally includes pointer upcast (converting from pointer-to-derived to pointer-to-base), in
the same way as allowed as an implicit conversion.
But dynamic_cast can also downcast (convert from pointer-to-base to pointer-to-derived)
polymorphic classes (those with virtual members) if -and only if- the pointed object is a valid
complete object of the target type. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// dynamic_cast
#include <iostream>
#include <exception>
using namespace std;
class Base { virtual void dummy() {} };
class Derived: public Base { int a; };
int main () {
try {
Base * pba = new Derived;
Base * pbb = new Base;
Derived * pd;
pd = dynamic_cast<Derived*>(pba);
if (pd==0) cout << "Null pointer on first type-
cast.\n";
pd = dynamic_cast<Derived*>(pbb);
if (pd==0) cout << "Null pointer on second type-
cast.\n";
} catch (exception& e) {cout << "Exception: " <<
e.what();}
return 0;
}
Null pointer on second type-
cast.
Compatibility note: This type of dynamic_cast requires Run-Time Type Information (RTTI) to keep
track of dynamic types. Some compilers support this feature as an option which is disabled by default.
This needs to be enabled for runtime type checking using dynamic_cast to work properly with these
types.
The code above tries to perform two dynamic casts from pointer objects of
type Base* (pba and pbb) to a pointer object of type Derived*, but only the first one is
successful. Notice their respective initializations:
1
2
Base * pba = new Derived;
Base * pbb = new Base;
Even though both are pointers of type Base*, pba actually points to an object of type Derived,
while pbb points to an object of type Base. Therefore, when their respective type-casts are
performed using dynamic_cast, pba is pointing to a full object of class Derived, whereas pbb is
pointing to an object of class Base, which is an incomplete object of class Derived.
When dynamic_cast cannot cast a pointer because it is not a complete object of the required
class -as in the second conversion in the previous example- it returns a null pointer to indicate
the failure. If dynamic_cast is used to convert to a reference type and the conversion is not
possible, an exception of type bad_cast is thrown instead.
dynamic_cast can also perform the other implicit casts allowed on pointers: casting null
pointers between pointers types (even between unrelated classes), and casting any pointer of any
type to a void* pointer.