The dynamic_cast operator (C++ only)
- A pointer to a base class to a pointer to a derived class
- An lvalue referring to a base class to an lvalue reference to a derived class
- An xvalue referring to a base class to an rvalue reference to a derived class
dynamic_cast operator syntax >>-dynamic_cast--<--T-->--(--v--)------------------------------><
With the right angle bracket feature, you may specify a template_id as T in the dynamic_cast operator with the >> token in place of two consecutive > tokens. For details, see Class templates (C++ only).
The expression dynamic_cast<T>(v) converts the expression v to type T. Type T must be a pointer or reference to a complete class type or a pointer to void.
- If T is a pointer type, v must be a (prvalue) rvalue, and dynamic_cast<T>(v) is a (prvalue) rvalue of type T.
- If T is an lvalue reference type, v must be an lvalue, and dynamic_cast<T>(v) is an lvalue of the type that is referred by T.
- If T is an rvalue reference type, dynamic_cast<T>(v) is an xvalue of the type that is referred by T.
If T is a pointer and the dynamic_cast operator fails, the operator returns a null pointer of type T. If T is a reference and the dynamic_cast operator fails, the operator throws the exception std::bad_cast. You can find this class in the standard library header <typeinfo>.
#include <iostream>
using namespace std;
struct A {
virtual ~A() { };
};
struct B : A { };
int main() {
B bobj;
A* ap = &bobj;
void * vp = dynamic_cast<void *>(ap);
cout << "Address of vp : " << vp << endl;
cout << "Address of bobj: " << &bobj << endl;
}
The output of this example is similar to the following
result. Both vp and &bobj refer
to the same address: Address of vp : 12FF6C
Address of bobj: 12FF6C
The primary purpose for the dynamic_cast operator is to perform type-safe downcasts. A downcast is the conversion of a pointer or reference to a class A to a pointer or reference to a class B, where class A is a base class of B. The problem with downcasts is that a pointer of type A* might point to an object that is not a base class subobject of type A that belongs to an object of type B or a class derived from B. The dynamic_cast operator ensures that if you convert a pointer to class A to a pointer to class B, the object of type A pointed to by the former belongs to an object of type B or a class derived from B as a base class subobject.
#include <iostream>
using namespace std;
struct A {
virtual void f() { cout << "Class A" << endl; }
};
struct B : A {
virtual void f() { cout << "Class B" << endl; }
};
struct C : A {
virtual void f() { cout << "Class C" << endl; }
};
void f(A* arg) {
B* bp = dynamic_cast<B*>(arg);
C* cp = dynamic_cast<C*>(arg);
if (bp)
bp->f();
else if (cp)
cp->f();
else
arg->f();
};
int main() {
A aobj;
C cobj;
A* ap = &cobj;
A* ap2 = &aobj;
f(ap);
f(ap2);
}
See the output of the above example: Class C
Class A
The function f() determines whether
the pointer arg points to an object of type A, B,
or C. The function does this by trying to convert arg to
a pointer of type B, then to a pointer of type C,
with the dynamic_cast operator. If the dynamic_cast operator
succeeds, it returns a pointer that points to the object denoted by arg.
If dynamic_cast fails, it returns 0.You may perform downcasts with the dynamic_cast operator only on polymorphic classes. In the above example, all the classes are polymorphic because class A has a virtual function. The dynamic_cast operator uses the runtime type information generated from polymorphic classes.