rtti behind the scenes

General opinion is rtti is bad for performance.

Rtti is implementation specific but I will try to discuss an approach:
Every object (with rtti support) has a pointer.
This pointer, points an array of virtual function pointers of the class. The vtable. There is one vtable per class (not per object).

Ιf you want rtti it is only one pointer comparision:
1
2
3
if (object1.vtable_pointer == object2.vtable_pointer) is_the_same_class();
// or
if (object1.vtable_pointer == Class::vtable_pointer) is_the_same_class();


So, why rtti has performance cost?
Last edited on
If you try to call any virtual function from a Base*, you will have to check to make sure it actually points to a Dervived*.

And what does line 3 mean anyway? That looks wrong to me.
Topic archived. No new replies allowed.