扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:李马编译 来源:VCKBASE 2007年10月19日
关键字:
#include <iostream> using namespace std; class Base1 { virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" << endl; } }; class Base2 { virtual void f() { cout << "Base2::f" << endl; } virtual void g() { cout << "Base2::g" << endl; } }; class Base3 { virtual void f() { cout << "Base3::f" << endl; } virtual void g() { cout << "Base3::g" << endl; } }; class Drive : public Base1, public Base2, public Base3 { public: virtual void fd() { cout << "Drive::fd" << endl; } virtual void gd() { cout << "Drive::gd" << endl; } }; typedef void(*Fun)(void); int main() { Drive objDrive; Fun pFun = NULL; // 调用Base1的第一个虚函数 pFun = (Fun)*((int*)*(int*)((int*)&objDrive+0)+0); pFun(); // 调用Base1的第二个虚函数 pFun = (Fun)*((int*)*(int*)((int*)&objDrive+0)+1); pFun(); // 调用Base2的第一个虚函数 pFun = (Fun)*((int*)*(int*)((int*)&objDrive+1)+0); pFun(); // 调用Base2的第二个虚函数 pFun = (Fun)*((int*)*(int*)((int*)&objDrive+1)+1); pFun(); // 调用Base3的第一个虚函数 pFun = (Fun)*((int*)*(int*)((int*)&objDrive+2)+0); pFun(); // 调用Base3的第二个虚函数 pFun = (Fun)*((int*)*(int*)((int*)&objDrive+2)+1); pFun(); // 调用派生类的第一个虚函数 pFun = (Fun)*((int*)*(int*)((int*)&objDrive+0)+2); pFun(); // 调用派生类的第二个虚函数 pFun = (Fun)*((int*)*(int*)((int*)&objDrive+0)+3); pFun(); return 0; } |
Base1::f Base1::g Base2::f Base2::f Base3::f Base3::f Drive::fd Drive::gd |
#include <iostream> using namespace std; class Base1 { public: virtual void f() { } }; class Base2 { public: virtual void f() { } }; class Base3 { public: virtual void f() { } }; class Drive : public Base1, public Base2, public Base3 { }; // 任意的非0值,因为0乘任何数都得0 #define SOME_VALUE 1 int main() { cout << (DWORD)static_cast<Base1*>((Drive*)SOME_VALUE)-SOME_VALUE << endl; cout << (DWORD)static_cast<Base2*>((Drive*)SOME_VALUE)-SOME_VALUE << endl; cout << (DWORD)static_cast<Base3*>((Drive*)SOME_VALUE)-SOME_VALUE << endl; return 0; } |
#define offsetofclass(base, derived) \ ((DWORD)(static_cast<base*>((derived*)_ATL_PACKING))-_ATL_PACKING) |
#include <windows.h> #include <iostream> using namespace std; class Base1 { public: virtual void f() { } }; class Base2 { public: virtual void f() { } }; class Base3 { public: virtual void f() { } }; class Drive : public Base1, public Base2, public Base3 { }; #define _ATL_PACKING 8 #define offsetofclass(base, derived) \ ((DWORD)(static_cast<base*>((derived*)_ATL_PACKING))-_ATL_PACKING) int main() { cout << offsetofclass(Base1, Drive) << endl; cout << offsetofclass(Base2, Drive) << endl; cout << offsetofclass(Base3, Drive) << endl; return 0; } |
0 4 8 |
#include <windows.h> #include <iostream> using namespace std; class Base1 { public: virtual void f() { } }; class Base2 { public: virtual void f() { } }; class Base3 { public: virtual void f() { } }; class Drive : public Base1, public Base2, public Base3 { }; #define BASE_OFFSET(ClassName, BaseName) \ (DWORD(static_cast<BaseName*>(reinterpret_cast<ClassName*>\ (0x10000000))) - 0x10000000) int main() { cout << BASE_OFFSET(Drive, Base1) << endl; cout << BASE_OFFSET(Drive, Base2) << endl; cout << BASE_OFFSET(Drive, Base3) << endl; return 0; } |
#include <windows.h> #include <iostream> using namespace std; class Base1 { public: virtual void f() { cout << "Base1::f()" << endl; } }; class Base2 { public: virtual void f() { cout << "Base2::f()" << endl; } }; class Base3 { public: virtual void f() { cout << "Base3::f()" << endl; } }; class Drive : public Base1, public Base2, public Base3 { }; #define _ATL_PACKING 8 #define offsetofclass(base, derived) \ ((DWORD)(static_cast<base*>((derived*)_ATL_PACKING))-_ATL_PACKING) int main() { Drive d; void* pVoid = NULL; // 调用Base1的函数 pVoid = (char*)&d + offsetofclass(Base1, Drive); ((Base1*)(pVoid))->f(); // 调用Base2的函数 pVoid = (char*)&d + offsetofclass(Base2, Drive); ((Base2*)(pVoid))->f(); // 调用Base3的函数(译注:原文为Base1) pVoid = (char*)&d + offsetofclass(Base3, Drive); ((Base3*)(pVoid))->f(); return 0; } |
Base1::f() Base2::f() Base3::f() |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者