标签:
1 #include <iostream> 2 using namespace std; 3 4 class Base 5 { 6 public: 7 virtual void f() { cout << "11 Base::f" << endl; } 8 virtual void g() { cout << "12 Base::g" << endl; } 9 virtual void h() { cout << "13 Base::h" << endl; } 10 }; 11 12 typedef void (*Fun)(void); 13 14 int main(void) 15 { 16 Fun pfun = NULL; 17 Base b; 18 cout << "1 pointer size:" << sizeof(&b) << ", int size:" << sizeof(int) << ", long size:" << sizeof(long) << endl; 19 cout << "2 &b: " << &b << endl; 20 cout << "3 (long*)(&b): " << (long*)(&b) << endl; 21 cout << "4 *(long*)(&b): " << *(long*)(&b) << endl; 22 cout << "5 (long*)*(long*)(&b): " << (long*)*(long*)(&b) << endl; 23 cout << "6 *(long*)*(long*)(&b): " << *(long*)*(long*)(&b) << endl; 24 cout << endl; 25 26 cout << "7 虚函数表地址即vptr的值(指针形式):" << (long*)*(long*)(&b) << endl; 27 cout << "8 虚函数表 第一个元素的值即第一个函数的地址(指针形式):" << (long*)*((long*)*(long*)(&b)+0) << endl; 28 cout << "9 虚函数表 第二个元素的值即第二个函数的地址(指针形式):" << (long*)*((long*)*(long*)(&b)+1) << endl; 29 cout << "10 虚函数表 第三个元素的值即第三个函数的地址(指针形式):" << (long*)*((long*)*(long*)(&b)+2) << endl; 30 // Base::f() 31 pfun = (Fun)*((long*)*(long*)(&b)+0); 32 pfun(); 33 // Base::g() 34 pfun = (Fun)*((long*)*(long*)(&b)+1); 35 pfun(); 36 // Base::h() 37 pfun = (Fun)*((long*)*(long*)(&b)+2); 38 pfun(); 39 return 0; 40 }
1 #include <iostream> 2 using namespace std; 3 4 class Base 5 { 6 public: 7 virtual void f() { cout << "Base::f" << endl; } 8 virtual void g() { cout << "Base::g" << endl; } 9 virtual void h() { cout << "Base::h" << endl; } 10 }; 11 12 class Derive: public Base 13 { 14 public: 15 virtual void f1() { cout << "Derive::f1" << endl; } 16 virtual void g1() { cout << "Derive::g1" << endl; } 17 virtual void h1() { cout << "Derive::h1" << endl; } 18 }; 19 20 typedef void (*Fun)(void); 21 22 int main(void) 23 { 24 Fun pfun = NULL; 25 Derive d; 26 27 pfun = (Fun)*((long*)*(long*)(&d)+0); // Base::f() 28 pfun(); 29 pfun = (Fun)*((long*)*(long*)(&d)+1); // Base::g() 30 pfun(); 31 pfun = (Fun)*((long*)*(long*)(&d)+2); // Base::h() 32 pfun(); 33 pfun = (Fun)*((long*)*(long*)(&d)+3); // Derive::f1() 34 pfun(); 35 pfun = (Fun)*((long*)*(long*)(&d)+4); // Derive::g1() 36 pfun(); 37 pfun = (Fun)*((long*)*(long*)(&d)+5); // Derive::h1() 38 pfun(); 39 40 return 0; 41 }
1 #include <iostream> 2 using namespace std; 3 4 class Base 5 { 6 public: 7 virtual void f() { cout << "Base::f" << endl; } 8 virtual void g() { cout << "Base::g" << endl; } 9 virtual void h() { cout << "Base::h" << endl; } 10 }; 11 12 class Derive: public Base 13 { 14 public: 15 virtual void f() { cout << "Derive::f" << endl; } 16 virtual void g1() { cout << "Derive::g1" << endl; } 17 virtual void h1() { cout << "Derive::h1" << endl; } 18 }; 19 20 typedef void (*Fun)(void); 21 22 int main(void) 23 { 24 Fun pfun = NULL; 25 Derive d; 26 27 pfun = (Fun)*((long*)*(long*)(&d)+0); // Derive::f() 28 pfun(); 29 pfun = (Fun)*((long*)*(long*)(&d)+1); // Base::g() 30 pfun(); 31 pfun = (Fun)*((long*)*(long*)(&d)+2); // Base::h() 32 pfun(); 33 pfun = (Fun)*((long*)*(long*)(&d)+3); // Derive::g1() 34 pfun(); 35 pfun = (Fun)*((long*)*(long*)(&d)+4); // Derive::h1() 36 pfun(); 37 38 return 0; 39 }
1 #include <iostream> 2 using namespace std; 3 4 class Base1 5 { 6 public: 7 virtual void f() { cout << "Base1::f" << endl; } 8 virtual void g() { cout << "Base1::g" << endl; } 9 virtual void h() { cout << "Base1::h" << endl; } 10 }; 11 12 class Base2 13 { 14 public: 15 virtual void f() { cout << "Base2::f" << endl; } 16 virtual void g() { cout << "Base2::g" << endl; } 17 virtual void h() { cout << "Base2::h" << endl; } 18 }; 19 20 class Base3 21 { 22 public: 23 virtual void f() { cout << "Base3::f" << endl; } 24 virtual void g() { cout << "Base3::g" << endl; } 25 virtual void h() { cout << "Base3::h" << endl; } 26 }; 27 28 class Derive: public Base1, public Base2, public Base3 29 { 30 public: 31 virtual void f1() { cout << "Derive::f1" << endl; } 32 virtual void g1() { cout << "Derive::g1" << endl; } 33 virtual void h1() { cout << "Derive::h1" << endl; } 34 }; 35 36 typedef void (*Fun)(void); 37 38 int main(void) 39 { 40 Fun pfun = NULL; 41 Derive d; 42 43 pfun = (Fun)*((long*)*((long*)(&d)+0)+0); // Base1::f() 44 pfun(); 45 pfun = (Fun)*((long*)*((long*)(&d)+0)+1); // Base1::g() 46 pfun(); 47 pfun = (Fun)*((long*)*((long*)(&d)+0)+2); // Base1::h() 48 pfun(); 49 pfun = (Fun)*((long*)*((long*)(&d)+0)+3); // Derive::f1() 50 pfun(); 51 pfun = (Fun)*((long*)*((long*)(&d)+0)+4); // Derive::g1() 52 pfun(); 53 pfun = (Fun)*((long*)*((long*)(&d)+0)+5); // Derive::h1() 54 pfun(); 55 56 pfun = (Fun)*((long*)*((long*)(&d)+1)+0); // Base2::f() 57 pfun(); 58 pfun = (Fun)*((long*)*((long*)(&d)+1)+1); // Base2::g() 59 pfun(); 60 pfun = (Fun)*((long*)*((long*)(&d)+1)+2); // Base2::h() 61 pfun(); 62 63 pfun = (Fun)*((long*)*((long*)(&d)+2)+0); // Base3::f() 64 pfun(); 65 pfun = (Fun)*((long*)*((long*)(&d)+2)+1); // Base3::g() 66 pfun(); 67 pfun = (Fun)*((long*)*((long*)(&d)+2)+2); // Base3::h() 68 pfun(); 69 70 return 0; 71 }
Derive d;
Base1 *b1 = &d;
Base2 *b2 = &d;
Base3 *b3 = &d;
b1->f(); //Derive::f()
b2->f(); //Derive::f()
b3->f(); //Derive::f()
b1->g(); //Base1::g()
b2->g(); //Base2::g()
1 #include <iostream> 2 using namespace std; 3 4 class Base1 5 { 6 public: 7 virtual void f() { cout << "Base1::f" << endl; } 8 virtual void g() { cout << "Base1::g" << endl; } 9 virtual void h() { cout << "Base1::h" << endl; } 10 }; 11 12 class Base2 13 { 14 public: 15 virtual void f() { cout << "Base2::f" << endl; } 16 virtual void g() { cout << "Base2::g" << endl; } 17 virtual void h() { cout << "Base2::h" << endl; } 18 }; 19 20 class Base3 21 { 22 public: 23 virtual void f() { cout << "Base3::f" << endl; } 24 virtual void g() { cout << "Base3::g" << endl; } 25 virtual void h() { cout << "Base3::h" << endl; } 26 }; 27 28 class Derive: public Base1, public Base2, public Base3 29 { 30 public: 31 void f() { cout << "Derive::f" << endl; } 32 virtual void g1() { cout << "Derive::g1" << endl; } 33 virtual void h1() { cout << "Derive::h1" << endl; } 34 }; 35 36 typedef void (*Fun)(void); 37 38 int main(void) 39 { 40 Fun pfun = NULL; 41 Derive d; 42 43 pfun = (Fun)*((long*)*((long*)(&d)+0)+0); // Derive::f() 44 pfun(); 45 pfun = (Fun)*((long*)*((long*)(&d)+0)+1); // Base1::g() 46 pfun(); 47 pfun = (Fun)*((long*)*((long*)(&d)+0)+2); // Base1::h() 48 pfun(); 49 pfun = (Fun)*((long*)*((long*)(&d)+0)+3); // Derive::g1() 50 pfun(); 51 pfun = (Fun)*((long*)*((long*)(&d)+0)+4); // Derive::h1() 52 pfun(); 53 54 pfun = (Fun)*((long*)*((long*)(&d)+1)+0); // Derive::f() 55 pfun(); 56 pfun = (Fun)*((long*)*((long*)(&d)+1)+1); // Base2::g() 57 pfun(); 58 pfun = (Fun)*((long*)*((long*)(&d)+1)+2); // Base2::h() 59 pfun(); 60 61 pfun = (Fun)*((long*)*((long*)(&d)+2)+0); // Derive::f() 62 pfun(); 63 pfun = (Fun)*((long*)*((long*)(&d)+2)+1); // Base3::g() 64 pfun(); 65 pfun = (Fun)*((long*)*((long*)(&d)+2)+2); // Base3::h() 66 pfun(); 67 68 return 0; 69 }
1 #include <iostream> 2 using namespace std; 3 4 class Base 5 { 6 private: 7 virtual void h() { cout << "Base::h" << endl; } 8 public: 9 virtual void f() { cout << "Base::f" << endl; } 10 protected: 11 virtual void g() { cout << "Base::g" << endl; } 12 }; 13 14 class Derive: public Base 15 { 16 public: 17 virtual void f() { cout << "Derive::f" << endl; } 18 protected: 19 virtual void g1() { cout << "Derive::g1" << endl; } 20 private: 21 virtual void h1() { cout << "Derive::h1" << endl; } 22 }; 23 24 typedef void (*pFun)(void); 25 26 int main(void) 27 { 28 pFun pfun = NULL; 29 Derive d; 30 // 通过访问虚函数表,在类外可以访问基类和子类中私有、保护类型的虚函数 31 pfun = (pFun)*((long*)*(long*)(&d)+0); // Base::h() 32 pfun(); 33 pfun = (pFun)*((long*)*(long*)(&d)+1); // Derive::f() 34 pfun(); 35 pfun = (pFun)*((long*)*(long*)(&d)+2); // Base::g() 36 pfun(); 37 pfun = (pFun)*((long*)*(long*)(&d)+3); // Derive::g1() 38 pfun(); 39 pfun = (pFun)*((long*)*(long*)(&d)+4); // Derive::h1() 40 pfun(); 41 42 Base *b = new Derive(); 43 // 基类指针无法访问子类中未覆盖父类的成员函数 44 b->g1(); // compile error: class Base has no member named g1 45 46 // 基类中的私有虚函数会被子类继承 47 d.h(); // compile error: virtual void Base::h() is private 48 d.g(); // compile error: virtual void Base::g() is protected 49 50 return 0; 51 }
标签:
原文地址:http://www.cnblogs.com/pluser/p/virtual_fun_table.html