C++ダミーテーブルアドレスオフセット
18338 ワード
#include
using namespace std;
class Base {
public:
Base() {
cout << "Base " << endl;
}
virtual ~Base() {
cout << "Base " << endl;
}
virtual void f1() {
cout << "Base::f1()" << endl;
}
void f2() {
cout << "Base::f2()" << endl;
}
virtual void f3() {
cout << "Base::f3()" << endl;
//cout << data << endl;
}
int data = 5;
static void fn() {
}
};
int main() {
cout << sizeof(Base) << endl;
Base* p1 = new Base();
Base* p2 = new Base();
cout << p1->fn << endl; //
cout << Base::fn << endl; //
// cout << &Base::f1 << endl; // void(__thiscall A::*)() bool
printf("Base::f1() :%p
", &Base::f1);
printf("Base::f1() :%p
", &Base::f1);
printf("Base::f2() :%p
", &Base::f2);
printf("Base::fn() :%p
", &Base::fn);
cout << "p1 : " << (p1) << endl;
cout << "p1 : " << (int*)(p1) << endl;
cout << "p1 1 : " << (int*)*(int*)(p1) << endl;
cout << "p1 2 : " << ((int*)*(int*)(p1)+1) << endl;
cout << "p1 3 : " << ((int*)*(int*)(p1)+2) << endl;
cout << "p1 4 : " << ((int*)*(int*)(p1)+3) << endl;
cout << "p2 : " << (p2) << endl;
cout << "p2 : " << (int*)(p2) << endl;
cout << "p2 1 : " << (int*)*(int*)(p2) << endl;
cout << "p2 2 : " << ((int*)*(int*)(p2)+1) << endl;
cout << "p2 3 : " << ((int*)*(int*)(p2)+2) << endl;
cout << "p2 4 : " << ((int*)*(int*)(p2)+3) << endl;
cout << endl;
typedef void(*PFUN)();
PFUN pfBase = (PFUN) * ((int*)*(int*)p1 + 0); //
PFUN pf1 = (PFUN) * ((int*)*(int*)p1 + 1); // f1
PFUN pf2 = (PFUN) * ((int*)*(int*)p1 + 2); // f2
PFUN pfnull = (PFUN) * ((int*)*(int*)p1 + 3); //0x00
pf1();
pf2(); // , 。 。 this 。
}
C/C++取得関数アドレス