Mu Qiao Mu Qiao - 1 month ago 18
C++ Question

Where is type_info object stored?

I read from "Inside The C++ Object Model" that the type_info object is often stored at the first slot of the virtual table. However, I iterated the members in the virtual table:

class Base {
public:
virtual void f() { cout << "Base::f" << endl; }
virtual void g() { cout << "Base::g" << endl; }
virtual void h() { cout << "Base::h" << endl; }
};

typedef void(*Fun)(void);

Base b;

(Fun)*((int*)*(int*)(&b)+0); // Base::f()
(Fun)*((int*)*(int*)(&b)+1); // Base::g()
(Fun)*((int*)*(int*)(&b)+2); // Base::h()


As you see from the last three lines, I can't find type_info at all.

Answer

There is no cross-compiler way to get at the type_info from the address of an object. Nor would you expect there to be; the way to get a type_info is using a specific C++ keyword: typeid.