RTTI
is a convenient extra feature, a bit of icing on the cake. Although normally
you upcast a pointer to a base class and then use the generic interface of that
base class (via virtual functions), occasionally you get into a corner where
things can be more effective if you know the exact type of the object pointed
to by the base pointer, and that’s what RTTI provides. Because some form
of virtual-function-based RTTI has appeared in almost all class libraries, this
is a useful feature because it means
You
don’t have to build it into your own libraries.
You
don’t have to worry whether it will be built into someone else’s
library.
You
don’t have the extra programming overhead of maintaining an RTTI scheme
during inheritance.
The
syntax is consistent, so you don’t have to figure out a new one for each
library.
While
RTTI is a convenience, like most features in C++ it can be misused by either a
naive or determined programmer. The most common misuse may come from the
programmer who doesn’t understand virtual functions and uses RTTI to do
type-check coding instead. The philosophy of C++ seems to be to provide you
with powerful tools and guard for type violations and integrity, but if you
want to deliberately misuse or get around a language feature, there’s
nothing to stop you. Sometimes a slight burn is the fastest way to gain
experience.
The
explicit cast syntax will be a big help during debugging because casting opens
a hole into your type system and allows errors to slip in. The explicit cast
syntax will allow you to more easily locate these error entryways.