C++ Reference Material
Standard I/O, File I/O and Run Time Type Identification (RTTI)
Sometimes, when a program runs, it does essentially the same thing with different kinds of data. Or, it may need to modify its behavior slightly, depending on the kind of data it is dealing with. Rather than write a separate routine to deal with each kind of data, it may be more programmer-efficient if only one routine is used, but that routine will have to be able to recognize (at runtime) just what kind of data it is currently dealing with, so that it can, in fact, respond "on the fly" in an appropriate manner. This is where the need for Run Time Type Identification comes in.
Here are three typical situations (among many) where such a need might arise:
Some programming languages, including C++, allow the programmer to examine the data type of a variable or object at runtime, and take appropriate action depending on the outcome of such a test. This facility may have to be "switched on", or it may be the default behavior. Since all run time tests are "costly", this too is a "costly" operation, and the better default may be to require its explicit invocation.
C++ has the keyword typeid, which is an operator that may take an expression or a typename as an argument. It returns a reference to an object of the type_info class, which requires that we #include the <typeinfo> header in order to use the typeid operator. A typical construct using typeid might look like this:
if (typeid(someObject) == typeid(SomeType)) { //Do whatever needs to be done if that object has that type ... }