The cast fails in both cases, as expected.
In the first case, it returns a nullptr
, which you should test against before de-referencing the pointer. In the second case, there isn't anything that can be returned, and the cast raises an std::bad_cast
exception. This what dynamic_cast
is designed to do.