RTTI implementation using typeid

I'm new to learning advanced C ++ topics, so please forgive me if the question sounds too obvious.

I read about various methods with which we can get information about the type of an object at runtime in C ++, which is usually called RTTI.

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

I want to know if using typeid is the correct way to implement RTTI in C ++, and if so, how can the typeid operator actually determine the type of an object at runtime (does bit patterns parse a blob object in memory?)

If the use of typeid is incorrect, try a different implementation method.

+2


source to share


2 answers


Important:
Ideally, if you need to define the type of an object, you need to rethink your design, because most likely you missed something and you are breaking SOLID OOP rules.

The C ++ standard provides dynamic_cast

both typeid

as two ways to determine the type of an object. Both have their own advantages and limitations. How they identify a type is implementation dependent, but they usually do so by storing a pointer to the type information structure in the object's vtable. If you are completely unaware of what a vtable is, Marshal Clines C ++ Faq gives a good explanation here .

You can find the implementation details used by most compilers in the C ++ Performance Technical Report

Corresponding extract:



5.3.7 Type information

Given an object of a polymorphic class (a class with at least one virtual function), the type_info object can be obtained using the typeid operator. Basically, this is a simple operation that involves looking up the virtual function table, locating the most derived object of the class of which the object is a part, and then retrieving a pointer to the type_info object from that virtual function table (or equivalent).


5.3.8 Dynamic Movie

Given a pointer to an object of a polymorphic class, casting a subobject of the same derived class object to a pointer to another base can be done using dynamic_cast. Basically, this operation involves looking up the virtual function table, thereby finding the most derived object of the class of which the object is a part, and then using the type information associated with that object to determine if conversion (casting) is allowed, and finally by making any necessary adjustments to this pointer. Basically, this check involves traversing the data structure that describes the base classes of the most derived class. Thus, the cost of dynamic_scanning at runtime may depend on the relative positions in the class hierarchy of the two participating classes.

+6


source


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



+1


source







All Articles