Run-time
type identification (RTTI) lets you find the exact type of an object when you
have only a pointer or reference to the base type.
This
can be thought of as a “secondary” feature in C++, a pragmatism to
help out when you get into messy situations. Normally, you’ll want to
intentionally ignore the exact type of an object and let the virtual function
mechanism implement the correct behavior for that type. But occasionally
it’s useful to know the exact type of an object for which you only have a
base pointer. Often this information allows you to perform a special-case
operation more efficiently or prevent a base-class interface from becoming
ungainly. It happens enough that most class libraries contain virtual functions
to produce run-time type information. When exception handling was added to C++,
it required the exact type information about objects. It became an easy next
step to build access to that information into the language.
This
chapter explains what RTTI is for and how to use it. In addition, it explains
the why and how of the new C++ cast syntax, which has the same appearance as
RTTI.