c++ RTTI相关代码
#include <iostream>
#include <memory>
#include <assert.h>
using namespace std;
struct fa
{
virtual void f() { cout << "fa:f" << endl; }
};
struct so :public fa
{
virtual void f() { cout << "son:f" << endl; fa::f();}
};
struct thr
{
virtual void f() { cout << "thr:f" << endl; }
};
typedef const struct _s_RTTICompleteObjectLocator {
unsigned long signature;
unsigned long offset;
unsigned long cdOffset;
int pTypeDescriptor; // Image relative offset of TypeDescriptor
int pClassDescriptor; // Image relative offset of _RTTIClassHierarchyDescriptor
int pSelf; // Image relative offset of this object
} _RTTICompleteObjectLocator;
int main()
{
so a;
fa b;
thr c;
a.f();
c.f();
_RTTICompleteObjectLocator * p = (_RTTICompleteObjectLocator*)((*((void***)&b))[-1]);
_RTTICompleteObjectLocator* p2 = (_RTTICompleteObjectLocator*)((*((void***)&a))[-1]);
_RTTICompleteObjectLocator* p3 = (_RTTICompleteObjectLocator*)((*((void***)&c))[-1]);
return 0;
}