If basePtr
was a pointer to an object that has since been deleted, it is not "safe" or well-defined to do anything at all with that dangling pointer value. typeid(*basePtr)
and dynamic_cast<T*>(basePtr)
are both undefined behavior, meaning the situation is worse than causing an exception: your program could crash, could do the wrong thing, or could appear to work for years and then suddenly break.
If you need to know about destruction of objects, this sounds like a case for std::shared_ptr
or std::weak_ptr
. Ordinary code should not use new expressions or delete expressions.