19.2.2. The typeid Operator

The second operator provided for RTTI is the typeid operator. The typeid operator allows a program to ask of an expression: What type is your object?

Exercises Section 19.2.1

Exercise 19.3: Given the following class hierarchy in which each class defines a public default constructor and virtual destructor:

class A { /* . . . */ };class B : public A { /* . . .  */ };class C : public B { /* . . .  */ };class D : public B, public A { /* . . .  */ };

which, if any, of the following dynamic_casts fail?

(a) A *pa = new C;    B *pb = dynamic_cast< B* >(pa);(b) B *pb = new B;    C *pc = dynamic_cast< C* >(pb);(c) A *pa = new D;    B *pb = dynamic_cast< B* >(pa);

Exercise 19.4: Using the classes defined in the first exercise, ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.