c++虚函数表详解(九)
来源:互联网 发布:unity3d中自动寻路 编辑:程序博客网 时间:2024/06/08 18:30
多重继承的派生类中存在和基类同名的虚函数的话,派生类的虚函数表是啥样的呢?
#include <iostream>#include <tchar.h>using namespace std;class Father{public:virtual void walk(){cout << _T("Father::walk") << endl;}virtual void speak(){cout << _T("Father::speak") << endl;}virtual void eat(){cout << _T("Father::eat") << endl;}};class Mother{public:virtual void walk(){cout << _T("Mother::walk") << endl;}virtual void speak(){cout << _T("Mother::speak") << endl;}virtual void eat(){cout << _T("Mother::eat") << endl;}};class Son: public Father, public Mother{public:virtual void speak(){cout << _T("Son::speak") << endl;}virtual void run(){cout << _T("Son::run") << endl;}};int _tmain(int argc, TCHAR argv[], TCHAR envp[]){Son Modi;int* pModi = (int*)(&Modi);int* AddrOfModiVTable1 = (int*)(*pModi);//从Father那里继承的虚函数表int* AddrOfModiVTable2 = (int*)(*(++pModi));//从Mother那里继承的虚函数表typedef void(*FUNC)();FUNC pFunc = (FUNC)(*AddrOfModiVTable1);// 从Father继承的虚函数表的第一项pFunc();pFunc = (FUNC)(*(++AddrOfModiVTable1));// 从Father继承的虚函数表的第二项pFunc();pFunc = (FUNC)(*(++AddrOfModiVTable1));// 从Father继承的虚函数表的第三项pFunc();pFunc = (FUNC)(*(++AddrOfModiVTable1));// 从Father继承的虚函数表的第四项pFunc();pFunc = (FUNC)(*AddrOfModiVTable2); // 从Mother继承的虚函数表的第一项pFunc();pFunc = (FUNC)(*(++AddrOfModiVTable2));// 从Mother继承的虚函数表的第二项pFunc();pFunc = (FUNC)(*(++AddrOfModiVTable2));// 从Mother继承的虚函数表的第三项pFunc();return 0;}
上述代码的执行结果如下图所示:
用图示解释如下:
图中省略了虚函数表中的NULL项
我们看到子类的同名虚函数的地址会覆盖所有基类的同名虚函数在虚函数表中的项的内容。
- c++虚函数表详解(九)
- C语言学习笔记(九)--函数
- C语言函数集(九)
- C语言基础知识之(九):函数
- 虚函数详解(C++)
- C Primer Plus(九) 函数
- C语言字符串函数(九)
- MATLAB 主要函数指令表(九)
- (九)SampleApp_ProcessEvent() 函数
- 九、函数(方法)
- Unix C (九)
- 【c++】打印九九口诀表
- C图形函数详解(转)
- (转)C语言sprintf函数详解
- C/C++中system()函数详解
- C语言字符串函数详解(转)
- C C++函数参数详解(转)
- c函数指针详解(转)
- Apache Log4j详解
- 当DiscuzNT遇上了Loadrunner(上)
- jquery
- sqlite数据库常用命令
- Java IO中字节流和字符流的区别
- c++虚函数表详解(九)
- 当DiscuzNT遇上了Loadrunner(中)
- 当DiscuzNT遇上了Loadrunner(下)
- Linux 内核的同步机制—第2部分
- t-sql中的随机数
- bash: id : command not found
- 对计算机专业学生的建议
- Problem 1 东风谷早苗(robot.cpp/c/pas)
- 对listBox控件 数据项进行操作 批量加入到另一个listBox列表框中