Compilation Errors That We Avoided By Using dynamic_cast
We avoid several compilation errors in this example by downcasting an
Employee pointer to a
BasePlusCommissionEmployee pointer (lines 38–39). If we remove the
dynamic_cast from line 39 and attempt to assign the current
Employee pointer directly to
derivedPtr, we’ll receive a compilation error. C++ does not allow a program to assign a base-class pointer to a derived-class pointer because the is-a relationship does not apply—a
CommissionEmployee is not a
BasePlusCommissionEmployee. The is-a relationship applies only between the derived class and its base classes, not vice versa.
Similarly, if lines 44, 46 and 48 used the current base-class pointer from ...