Question

I am a newbie in the study of advanced C++ topics so please pardon me if the question sounds too obvious.

I have been reading about the various methods by which we can get the information of the type of the object at runtime in C++, which is generally referred to as RTTI.

But, I am confused as to how does it work. I have read some of things that are often mentioned when RTTI is being explained. One of them is the use of dynamic_cast<> to cast an object to some other object dynamically. Another is the use of typeid to determine the type of the object at runtime.

I want to know whether using typeid is the correct way to implement RTTI in C++ and if yes, then how is typeid operator in fact able to determine the type of the object at run time ( Does it analyse the bit patterns of the object blob in memory? )

If using typeid is not the correct way, then please suggest some other method for its implementation.

Was it helpful?

Solution

Important:
Ideally, if you need to identify the type of an object you need to revisit your design because most likely you missed something there and you are violating the SOLID rules of OOP.

C++ standard provides dynamic_cast and typeid as two ways of determining the type of an object. Both have their advantages and limitations. How they identify the type is an implementation dependent detail but typically they do so by maintaining a pointer to the type information structure in the vtable of an object. If you are completely unaware of what a vtable is, Marshal Clines C++ Faq provides a good explanation here.

You can find the implementation detail used by most compilers in,
Technical Report on C++ Performance

Relevant Extract:

5.3.7 Type Information

Given an object of a polymorphic class (a class with at least one virtual function), a type_info object can be obtained through the use of the typeid operator. In principle, this is a simple operation which involves finding the virtual function table, through that finding the most-derived class object of which the object is part, and then extracting a pointer to the type_info object from that object’s virtual function table (or equivalent).


5.3.8 Dynamic Cast

Given a pointer to an object of a polymorphic class, a cast to a pointer to another base subobject of the same derived class object can be done using a dynamic_cast. In principle, this operation involves finding the virtual function table, through that finding the most-derived class object of which the object is part, and then using type information associated with that object to determine if the conversion (cast) is allowed, and finally performing any required adjustments of the this pointer. In principle, this checking involves the traversal of a data structure describing the base classes of the most derived class. Thus, the run-time cost of a dynamic_cast may depend on the relative positions in the class hierarchy of the two classes involved.

OTHER TIPS

RTTI only works with instances of classes which have virtual functions. In this case, the compiler adds a special member to the class call the virtual table pointer. Each class that has virtual functions has its own virtual table. By examining which virtual table is pointed to, the specific type of the object can be determined.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top