As I understand it, what makes dynamic cast different from a static cast is its use of RTTI, and the fact that it fails if the dynamic type of a variable- when casting from base to derived- does not fit. But why does the class have to be polymorphic for that to be done if we have the RTTI anyway?
EDIT: Since there was some confusion about the use of the word “polymorphic”, here’s the entry in cplusplus.com that prompted me to ask this:
dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.
Therefore, dynamic_cast is always successful when we cast a class to one of its base
classes: class CBase { };
class CDerived: public CBase { };
CBase b; CBase* pb; CDerived d;
CDerived* pd;
pb = dynamic_cast<CBase*>(&d); //ok: derived-to-base
pd = dynamic_cast<CDerived*>(&b); //wrong: base-to-derived
The second conversion in this piece of code would produce a compilation error since base-to-derived conversions are not allowed with dynamic_cast unless the base class is polymorphic.
What sort of pointer could you use if there was no inheritance relationship? The only legal and sensible casts that can be performed between pointers to objects of different types (ignoring const casts) are within the same inheritance hierarchy.
Edit: To quote BS from the D&E book on
dynamic_cast, section 14.2.2.2:My emphasis.