接前面虚表的内存分布,今天重点看多重继承的虚表内存分布,简单的说,继承几个类便有几个虚表,如下代码

class Drive : public Base1, public Base2, public Base3 {
public:
virtual void fd() { cout << "Drive::fd" << endl; }
virtual void gd() { cout << "Drive::gd" << endl; }
};

则虚表有3个,如下图所示:

添加更多的测试代码:

// trytest.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <WTypes.h>
#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;
cout << "Size is = " << sizeof(objDrive) << endl; Fun pFun = NULL; cout<<"sizeof(int)="<<sizeof(int)<<endl;
cout<<"sizeof(int*)="<<sizeof(int*)<<endl; // 调用Base1的第一个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
// 调用Base1的第二个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
cout << "Address of virtual pointer 1 " << (int*)(&objDrive + ) << endl;
cout << "Value at virtual pointer i.e. Address of virtual table "
<< (int*)*(int*)((int*)&objDrive + ) << endl;
cout << "Information about VTable 1" << endl;
cout << "Value at 1st entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl;
cout << "Value at 2nd entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl; // 调用Base2的第一个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
// 调用Base2的第二个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
cout << "Address of virtual pointer 2 " << (int*)((int*)&objDrive + ) << endl;
cout << "Value at virtual pointer i.e. Address of virtual table "
<< (int*)*(int*)((int*)&objDrive + ) << endl;
cout << "Information about VTable 2" << endl;
cout << "Value at 1st entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl;
cout << "Value at 2nd entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl; // 调用Base3的第一个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
// 调用Base3的第二个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
cout << "Address of virtual pointer 3 " << (int*)((int*)&objDrive + ) << endl;
cout << "Value at virtual pointer i.e. Address of virtual table "
<< (int*)*(int*)((int*)&objDrive + ) << endl;
cout << "Information about VTable 3" << endl;
cout << "Value at 1st entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl;
cout << "Value at 2nd entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl; // 调用派生类的第一个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
// 调用派生类的第二个虚函数
pFun = (Fun)*((int*)*(int*)((int*)&objDrive + ) + );
pFun();
cout << "Value at 3rd entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl;
cout << "Value at 4th entry of VTable "
<< (int*)*((int*)*(int*)((int*)&objDrive + ) + ) << endl; //明确多重继承中虚函数的内存位置
cout <<"Address at 1st entry of VTable "
<< (int*)*((int*)&objDrive + ) + << endl; cout <<"Address at 2nd entry of VTable "
<< (int*)*((int*)&objDrive + ) + << endl; cout <<"Address at 3rd entry of VTable "
<< (int*)*((int*)&objDrive + ) + << endl; cout <<"Address at 4th entry of VTable "
<< (int*)*((int*)&objDrive + ) + << endl; pFun = (Fun)*((int*)*((int*)&objDrive + ) + );
pFun(); pFun = (Fun)*((int*)*((int*)&objDrive + ) + );
pFun(); pFun = (Fun)*((int*)*((int*)&objDrive + ) + );
pFun(); pFun = (Fun)*((int*)*((int*)&objDrive + ) + );
pFun(); return ;
}

内存分布如下所示:

C++多重继承虚表的内存分布的更多相关文章

  1. 【转】C++类-内存分布

    C++类内存分布 - 转载自Jerry19880126 - 博客园 的文章 在上面这篇文章的基础上做了些整理. 主要讨论了C++类对象的内存分布结构. 来看看编译器是怎么处理类成员内存分布的,特别是在 ...

  2. C++类内存分布

    http://www.cnblogs.com/jerry19880126/p/3616999.html#undefined 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来看看 ...

  3. C++ 类的内存分布

    C++类内存分布 转自:http://www.cnblogs.com/jerry19880126/p/3616999.html   先写下总结,通过总结下面的例子,你就会明白总结了. 下面总结一下: ...

  4. 转载:C++类内存分布

    本文转自:http://www.cnblogs.com/jerry19880126/p/3616999.html,原文写的非常好,从中学到了虚继承的概念,也学会了用VS查看内存分布. 说下C++内存分 ...

  5. 【C++ Primer | 15】C++类内存分布

    C++类内存分布 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来看看编译器是怎么处理类成员内存分布的,特别是在继承.虚函数存在的情况下. 下面可以定义一个类,像下面这样: c ...

  6. 【转】C++类内存分布

    C++类内存分布  https://www.cnblogs.com/jerry19880126/p/3616999.html 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来看 ...

  7. c++类内存分布解析

    首先使用Visual Studio工具查看类的内存分布,如下: 先选择左侧的C/C++->命令行,然后在其他选项这里写上/d1 reportAllClassLayout,它可以看到所有相关类的内 ...

  8. C++类虚函数内存分布(这个 你必须懂)

    转自:http://www.cnblogs.com/jerry19880126/p/3616999.html C++类内存分布 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来 ...

  9. 使用Visual Studio查看C++类内存分布

    书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来看看编译器是怎么处理类成员内存分布的,特别是在继承.虚函数存在的情况下. 工欲善其事,必先利其器,我们先用好Visual Stu ...

随机推荐

  1. java学习之面向对象概念

    思考的两种方式: 举例: 把大象放到冰箱里 一.面向过程 :[打开冰箱->把大象放里面->关上冰箱门]面向过程注重的是过程,也就是(动作[函数]),然后按照动作依次去执行就好了. 代表语言 ...

  2. bzoj1978

    朴素的算法是O(n2logn)观察这个算法,似乎很难在进行优化我们就要换一种思路考虑到一个数的约数总不是很多,穷举约数也是可以在O(sqrt(x))的时间内完成的并且注意到,能否继续往下选数,只在于最 ...

  3. 利用matlab给图像加高斯噪声

    I = imread('DSC_0034.JPG'); J = imnoise(I,'gaussian',0.20); figure, imshow(I), figure, imshow(J)

  4. SQL时间格式化

    1 取值后格式化 {:d}小型:如2005 {:D}大型:如2005年5月6日 {:f}完整型 2 当前时间获取 DateTime.Now.ToShortDateString 3 取值中格式化SQL ...

  5. 【Android Studio】No JVM installation found

    如果没有配置好JDK的环境变量,启动Android Studio的时候会报错: 请参考我整理的博客文章<JDK的下载.安装和配置>,链接:http://www.cnblogs.com/du ...

  6. 手把手教你把VIM改成一个集成开发环境

    转载自:http://blog.csdn.net/wooin/article/details/1858917 在原文基础上经过自己验证,修改和完善 OS:Fedora19 VIM:VIM7.4 一.写 ...

  7. 安卓四大组件之一activity

    概要说明 Activity是Android组件中最基本也是最为常见用的四大组件(Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播 ...

  8. [PWA] 16. Clean IDB

    When we save items to IndexDB, we need to think about clean the items or keep those in a short list. ...

  9. java内存不足

    -Xmx1024m -Xms1024m -XX:PermSize=128m -XX:MaxPermSize=512m ------------------------- 亲测可用

  10. [转] Java快速教程

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Java是面向对象语言.这门语言其实相当年轻,于1995年才出现,由Sun公司出品 ...