"Safe" dynamic cast?

c++, casting, oop

Solution

When `dynamic_cast` cannot cast a pointer because it is not a complete object of the required class 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.

Problem

I'm familiar with how to do a dynamic cast in C++, as follows: ``` myPointer = dynamic_cast<Pointer*>(anotherPointer); ``` But how do you make this a "safe" dynamic cast?

Original source