虚函数的实现机制
来源:互联网 发布:植物大战僵尸2淘宝存档 编辑:程序博客网 时间:2024/04/27 16:52
#include <iostream>using namespace std;class CBase1 {private:int a;int b;void sayBye() { cout << "Bye, CBase1!" << endl;}public:CBase1() {cout << "Hello CBase1!" << endl;}virtual ~CBase1() {cout << "Bye CBase1!" << endl;}virtual void sayHi() { cout << "Hello, CBase1!" << endl; sayBye();}};class CBase2 {private:int a;int b;public:CBase2() {cout << "Hello CBase2!" << endl;}~CBase2() {cout << "Bye CBase2!" << endl;}virtual void sayHi() { cout << "Hello, CBase2!" << endl;}};class CSub2: public CBase2 { //继承方式public:CSub2() {cout << "Hello CSub2" << endl;}~CSub2() {cout << "Bye CSub2" << endl;}};class CSub1: public CBase1, public CBase2 { //继承方式public:CSub1() {cout << "Hello CSub1" << endl;}~CSub1() {cout << "Bye CSub1" << endl;} void sayHi() { cout << "Hello, CSub1!" << endl; } void sayLove() { cout << "I love you!" << endl; }private:};int main(){ CBase1* a = new CSub1(); a->sayHi(); dynamic_cast<CSub1*>(a)->sayLove(); return 0;}
#include <iostream>using namespace std;class Base1 {public: virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" << endl; } virtual void h() { cout << "Base1::h" << endl; }};class Base2 {public: virtual void f() { cout << "Base2::f" << endl; } virtual void g() { cout << "Base2::g" << endl; } virtual void h() { cout << "Base2::h" << endl; }};class Base3 {public: virtual void f() { cout << "Base3::f" << endl; } virtual void g() { cout << "Base3::g" << endl; } virtual void h() { cout << "Base3::h" << endl; }};class Derive : public Base1, public Base2, public Base3 {public: virtual void f() { cout << "Derive::f" << endl; } virtual void g1() { cout << "Derive::g1" << endl; }};typedef void(*Fun)(void);int main(){ Fun pFun = NULL; Derive d; int** pVtab = (int**)&d; //Base1's vtable //pFun = (Fun)*((int*)*(int*)((int*)&d+0)+0); pFun = (Fun)pVtab[0][0]; pFun(); //pFun = (Fun)*((int*)*(int*)((int*)&d+0)+1); pFun = (Fun)pVtab[0][1]; pFun(); //pFun = (Fun)*((int*)*(int*)((int*)&d+0)+2); pFun = (Fun)pVtab[0][2]; pFun(); //Derive's vtable //pFun = (Fun)*((int*)*(int*)((int*)&d+0)+3); pFun = (Fun)pVtab[0][3]; pFun(); //The tail of the vtable pFun = (Fun)pVtab[0][4]; cout<<pFun<<endl; //Base2's vtable //pFun = (Fun)*((int*)*(int*)((int*)&d+1)+0); pFun = (Fun)pVtab[1][0]; pFun(); //pFun = (Fun)*((int*)*(int*)((int*)&d+1)+1); pFun = (Fun)pVtab[1][1]; pFun(); pFun = (Fun)pVtab[1][2]; pFun(); //The tail of the vtable pFun = (Fun)pVtab[1][3]; cout<<pFun<<endl; //Base3's vtable //pFun = (Fun)*((int*)*(int*)((int*)&d+1)+0); pFun = (Fun)pVtab[2][0]; pFun(); //pFun = (Fun)*((int*)*(int*)((int*)&d+1)+1); pFun = (Fun)pVtab[2][1]; pFun(); pFun = (Fun)pVtab[2][2]; pFun(); //The tail of the vtable pFun = (Fun)pVtab[2][3]; cout<<pFun<<endl; /* Derive::f Base1::g Base1::h Derive::g1 1 Derive::f Base2::g Base2::h 1 Derive::f Base3::g Base3::h 0 */ return 0;}
0 0
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- 虚函数的实现机制
- Android4.3的新类UiAutomationConnection
- 6.栈的应用-迷宫求解
- 【源码学习-LinkedHashMap】
- python处理html中的转义字符
- LIGHTOJ 1044(动态规划)
- 虚函数的实现机制
- poj-1149-PIGS-网络流
- 解决ListView中嵌套的GridView显示不正常的问题
- 线性回归和逻辑回归总结
- Android自动化基于UIAutomator的实现及代码生成
- 利用Telnet来模拟Http请求 有GET和POST两种
- 半平面相交的排序增量法
- 在 td 中加valign=‘top’ 可以让里面的内容置顶
- GNU ARM命令行工具