C++ Programming/RTTI

Run-Time Type Information (RTTI)
RTTI refers to the ability of the system to report on the dynamic type of an object and to provide information about that type at runtime (as opposed to at compile time), and when utilized consistently can be a powerful tool to ease the work of the programmer in managing resources.

Consider what you have already learned about the keyword, and let's say that we have the following class hierarchy:

Let's say that we also have a pointer of type, like so:

Supposing that a situation emerges that we are forced to presume but have no guarantee that the pointer points to an object of type  and we would like to call the member   of that class. To dynamically convert to a derived type we can use, like so:

With, the program converts the base class pointer to a derived class pointer and allows the derived class members to be called. Be very careful, however: if the pointer that you are trying to cast is not of the correct type, then will return a null pointer.

We can also use with references.

This works almost in the same way as pointers. However, if the real type of the object being cast is not correct then will not return null (there's no such thing as a null reference). Instead, it will throw a  exception.

In RTTI it is used in this setup:

Sometimes we need to know the exact type of an object. The  operator returns a reference to a standard class   that contains information about the type. This class provides some useful members including the  and   operators. The most interesting method is probably:

This member function returns a pointer to a C-style string with the name of the object type. For example, using the classes from our earlier example:

This program would print something like because that is the dynamic type of the pointer.

is actually an operator rather than a function, as it can also act on types:

for example (and somewhat circularly)

will give a  object which describes   objects. This latter use is not RTTI, but rather CTTI (compile-time type identification).

Limitations
There are some limitations to RTTI. First, RTTI can only be used with polymorphic types. That means that your classes must have at least one virtual function, either directly or through inheritance. Second, because of the additional information required to store types some compilers require a special switch to enable RTTI.

Note that references to pointers will not work under RTTI:

Will report, as   does not support reference types.

Misuses of RTTI
RTTI should only be used sparingly in C++ programs. There are several reasons for this. Most importantly, other language mechanisms such as polymorphism and templates are almost always superior to RTTI. As with everything, there are exceptions, but the usual rule concerning RTTI is more or less the same as with  statements. Do not use it as a shortcut around proper, more robust design. Only use RTTI if you have a very good reason to do so and only use it if you know what you are doing.