21.5 (cont.) RTTI - the Dynamic Cast

Syntax 21.2: Dynamic Cast
dynamic_cast<type_name>(expression)
Example:
Employee* e = new Manager("Sarah Smith", 67000, 2000);
Manager* m = dynamic_cast<Manager*>(e);
Purpose: Safely test the type of a polymorphic variable, converting to a derived class type if appropriate, returning a Null pointer if not.

prev |top |next