赞
踩
- #include <iostream>
-
- using namespace std;
-
- class Base
- {
- public:
- virtual void func1() { cout << "Base::fun1" << endl;}
- virtual void func2() { cout << "Base::fun2" << endl;}
- virtual void func3() { cout << "Base::fun3" << endl;}
- private:
- int num1;
- int num2;
- };
-
- // 函数指针
- typedef void (*Fun)();
-
- int main()
- {
-
- Base b;
-
- Fun pFun;
-
- //取得Base::func1()的地址
- pFun = (Fun)*((int*)*(int*)(&b) + 0);
- pFun();
-
- //取得Base::func2()的地址
- pFun = (Fun)*((int*)*(int*)(&b) + 1);
- pFun();
-
- //取得Base::func3()的地址
- pFun = (Fun)*((int*)*(int*)(&b) + 2);
- pFun();
-
- return 0;
- }

- (Fun)*((int*)*(int*)(&b) + 0);
-
- // 取得对象b的地址,转换为int*, 又因为虚函数表在类的其实位置, 因此这一步也是取得虚函数表的地址
- (int*)(&b);
-
- // 偏移都虚函数表的第一个函数处
- (int*)(&b) + 0;
-
- // 取得第一个函数的地址
- *(int*)(&b) + 0;
-
- // 将第一个函数的地址转换为int*
- (int*)*(int*)(&b) + 0;
-
- // 取得地址中的函数
- *((int*)*(int*)(&b) + 0);
-
- // 转换为Fun函数
- (Fun)*((int*)*(int*)(&b) + 0);

- #include <iostream>
-
- using namespace std;
-
- class Base1
- {
- public:
- Base1(int num) : num_1(num) { }
- virtual void foo1() { cout << "Base1::foo1" << endl;}
- virtual void foo2() { cout << "Base1::foo2" << endl;}
- virtual void foo3() { cout << "Base1::foo3" << endl;}
- private:
- int num_1;
- };
-
- class Derived1 : public Base1
- {
- public:
- Derived1(int num) : Base1(num) { }
- virtual void faa1() { cout << "Derived1::faa1" << endl;}
- virtual void faa2() { cout << "Derived1::faa2" << endl;}
- };

- #include <iostream>
-
- using namespace std;
-
- class Base1
- {
- public:
- Base1(int num) : num_1(num) { }
- virtual void foo1() { cout << "Base1::foo1" << endl;}
- virtual void foo2() { cout << "Base1::foo2" << endl;}
- virtual void foo3() { cout << "Base1::foo3" << endl;}
- private:
- int num_1;
- };
-
- class Derived2 : public Base1
- {
- public:
- Derived2(int num) : Base1(num) { }
- virtual void foo2() { cout << "Derived2::foo2" << endl;} //重写了基类的foo2虚函数
- virtual void fbb2() { cout << "Derived2::fbb2" << endl;}
- virtual void fbb3() { cout << "Derived2::fbb3" << endl;}
- };

- Derived2 *p = new Base1(1);
-
- p->foo2(); //调用派生类的foo2()函数
- #include <iostream>
-
- using namespace std;
-
- class Base1
- {
- public:
- Base1(int num) : num_1(num) { }
- virtual void foo1() { cout << "Base1::foo1" << endl;}
- virtual void foo2() { cout << "Base1::foo2" << endl;}
- virtual void foo3() { cout << "Base1::foo3" << endl;}
- private:
- int num_1;
- };
-
- class Base2
- {
- public:
- Base2(int num) : num_2(num) { }
- virtual void foo1() { cout << "Base2::foo1" << endl;}
- virtual void foo2() { cout << "Base2::foo2" << endl;}
- virtual void foo3() { cout << "Base2::foo3" << endl;}
- private:
- int num_2;
- };
-
- class Base3
- {
- public:
- Base3(int num) : num_3(num) { }
- virtual void foo1() { cout << "Base3::foo1" << endl;}
- virtual void foo2() { cout << "Base3::foo2" << endl;}
- virtual void foo3() { cout << "Base3::foo3" << endl;}
- private:
- int num_3;
- };
-
- class Derived3 : public Base1, public Base2, public Base3
- {
- public:
- Derived3(int num1, int num2, int num3) : Base1(num1), Base2(num2), Base3(num3) { }
- virtual void fcc1() { cout << "Derived3::fcc1" << endl;}
- virtual void fcc2() { cout << "Derived3::fcc2" << endl;}
- };

- Base2 *pBase2 = new Derived3();
- pBase2->foo2(); //调用Base2::foo2()
- #include <iostream>
-
- using namespace std;
-
- class Base1
- {
- public:
- Base1(int num) : num_1(num) { }
- virtual void foo1() { cout << "Base1::foo1" << endl;}
- virtual void foo2() { cout << "Base1::foo2" << endl;}
- virtual void foo3() { cout << "Base1::foo3" << endl;}
- private:
- int num_1;
- };
-
- class Base2
- {
- public:
- Base2(int num) : num_2(num) { }
- virtual void foo1() { cout << "Base2::foo1" << endl;}
- virtual void foo2() { cout << "Base2::foo2" << endl;}
- virtual void foo3() { cout << "Base2::foo3" << endl;}
- private:
- int num_2;
- };
-
- class Base3
- {
- public:
- Base3(int num) : num_3(num) { }
- virtual void foo1() { cout << "Base3::foo1" << endl;}
- virtual void foo2() { cout << "Base3::foo2" << endl;}
- virtual void foo3() { cout << "Base3::foo3" << endl;}
- private:
- int num_3;
- };
-
- class Derived4 : public Base1, public Base2, public Base3
- {
- public:
- Derived4(int num1, int num2, int num3) : Base1(num1), Base2(num2), Base3(num3) { }
- // 重写了Base1::foo1()、Base2::foo1()、Base3::foo1()
- virtual void foo1() { cout << "Derived4::foo1" << endl;}
- virtual void fdd() { cout << "Derived4::fdd" << endl;}
- };

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。