| <<< Safer casting with static_cast | Index | Unrelated types conversion: reinterpret_cast >>> |
Use dynamic_cast when you are converting up or down a type hierarchy and want runtime checking:
Derived* dp;
// Trust the compiler:
Base* bp = dynamic_cast< Base* >( dp ); // surely safe
Derived* dp = dynamic_cast< Derived* >( bp );
if ( dp != NULL ) {
// cast worked fine:
//...
} else {
// *bp wasn't really a Derived object!!
//...
dynamic_cast is an operator that requires a polymorphic operand.
It comes with a small run-time cost overhead.
Safe and portable.
| <<< Safer casting with static_cast | Index | Unrelated types conversion: reinterpret_cast >>> |