Implementation of RTTI using typeid
Asked Answered
A

2

5

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.

Antwanantwerp answered 28/4, 2013 at 18:37 Comment(7)
It looks like you got it backwards. typeid can work because RTTI is implemented in the compiler, so asking whether or not to use typeid "to implement RTTI" looks confused. What exactly do you mean by "implement RTTI"?Quick
What problem are you trying to solve that makes you think you need RTTI and/or typeid?Aggregate
The C++ RTTI system is a very poor and feature-less variant of reflection. My personal opinion is that you should stay away from any kind of reflective need in your design as much as possible, no matter the language.Eulogist
I am just trying to get a good grasp in the concept of RTTI. I have read at many places that typeid operator can be used to determine the run time type of the object in question (Of course, the question of determining the object type would come when inheritance is in picture). But, I am not very much what does dynamic_cast has to do with RTTI.Antwanantwerp
Both dynamic_cast and typeid depend on the language feature called RTTI. If your source code uses either of them, you need RTTI for the program to compile. However, RTTI demands a small runtime overhead so some compilers allow you to turn it off. This is the reason why we want a common name for "typeid and dynamic_cast", instead of just talking about each of them in isolation.Quick
@MagnusHoff: What you explained has calmed my nerves down a bit! But, as far as I have understood dynamic_cast is used to typecast an object to some other type than it's currently of. So, what is the need of determining the run time type of the object in this case. The compiler could just have cast the object to desired type without examining the current type. Am I missing some undermine implementation?Antwanantwerp
Roughly speaking, the dynamic_cast will fail if the object is not of the target type. Please ask a new question (with the Ask Question-button) if you want to clear up how dynamic_cast works, so we can keep the questions and answers on point, and avoid long comment threads :)Quick
S
10

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.

Solidary answered 28/4, 2013 at 18:42 Comment(1)
In the extract provided here, it is mentioned that RTTI uses a simple vtable to get type info. Anyways classes having virtual functions already have a vtable. Then how does RTTI cause additional performance overhead?Peddler
R
3

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.

Rosen answered 28/4, 2013 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.