19.2.3. Using RTTI
As an example of when RTTI might be useful, consider a class hierarchy for which we’d like to implement the equality operator (§ 14.3.1, p. 561). Two objects are equal if they have the same type and same value for a given set of their data members. Each derived type may add its own data, which we will want to include when we test for equality.
Exercise 19.6: Write an expression to dynamically cast a pointer to a Query_base
to a pointer to an AndQuery
(§ 15.9.1, p. 636). Test the cast by using objects of AndQuery
and of another query type. Print a statement indicating whether the cast works and be sure that the output matches your expectations.
Exercise 19.7: Write the same cast, but cast a Query_base ...
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.