C++多重继承虚表的内存分布
接前面虚表的内存分布,今天重点看多重继承的虚表内存分布,简单的说,继承几个类便有几个虚表,如下代码
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++多重继承虚表的内存分布的更多相关文章
- 【转】C++类-内存分布
		
C++类内存分布 - 转载自Jerry19880126 - 博客园 的文章 在上面这篇文章的基础上做了些整理. 主要讨论了C++类对象的内存分布结构. 来看看编译器是怎么处理类成员内存分布的,特别是在 ...
 - C++类内存分布
		
http://www.cnblogs.com/jerry19880126/p/3616999.html#undefined 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来看看 ...
 - C++ 类的内存分布
		
C++类内存分布 转自:http://www.cnblogs.com/jerry19880126/p/3616999.html 先写下总结,通过总结下面的例子,你就会明白总结了. 下面总结一下: ...
 - 转载:C++类内存分布
		
本文转自:http://www.cnblogs.com/jerry19880126/p/3616999.html,原文写的非常好,从中学到了虚继承的概念,也学会了用VS查看内存分布. 说下C++内存分 ...
 - 【C++ Primer | 15】C++类内存分布
		
C++类内存分布 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来看看编译器是怎么处理类成员内存分布的,特别是在继承.虚函数存在的情况下. 下面可以定义一个类,像下面这样: c ...
 - 【转】C++类内存分布
		
C++类内存分布 https://www.cnblogs.com/jerry19880126/p/3616999.html 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来看 ...
 - c++类内存分布解析
		
首先使用Visual Studio工具查看类的内存分布,如下: 先选择左侧的C/C++->命令行,然后在其他选项这里写上/d1 reportAllClassLayout,它可以看到所有相关类的内 ...
 - C++类虚函数内存分布(这个 你必须懂)
		
转自:http://www.cnblogs.com/jerry19880126/p/3616999.html C++类内存分布 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来 ...
 - 使用Visual Studio查看C++类内存分布
		
书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来看看编译器是怎么处理类成员内存分布的,特别是在继承.虚函数存在的情况下. 工欲善其事,必先利其器,我们先用好Visual Stu ...
 
随机推荐
- Apache HTTP Server mod_session_dbd 远程安全漏洞(CVE-2013-2249)
			
漏洞版本: Apache 2.4.2 漏洞描述: BUGTRAQ ID: 61379 CVE(CAN) ID: CVE-2013-2249 Apache HTTP Server是开源HTTP服务器. ...
 - CodeForce 2A  Winner
			
很多人玩一个游戏,每一轮有一个人得分或者扣分,最后分数最高的人夺冠:如果最后有多个人分数都是最高的,则这些人里面,在比赛过程中首先达到或者超过这个分数的人夺冠.现在给定最多1000轮每轮的情况,求最后 ...
 - (转载)mysql查询一天,查询一周,查询一个月的数据
			
(转载)http://www.cnblogs.com/likwo/archive/2010/04/16/1713282.html 查询一天: select * from table where to_ ...
 - HDOJ(HDU) 2317 Nasty Hacks(比较、)
			
Problem Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of mali ...
 - 暴力求解——UVA 572(简单的dfs)
			
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
 - [Locked] Zigzag Iterator
			
Zigzag Iterator Given two 1d vectors, implement an iterator to return their elements alternately. Fo ...
 - selenium grid 搭建
			
hub端 Step1: 下载 selenium-server-standalone-x.xx.x.jar我下载的是:selenium-server-standalone-2.44.0.jar下载地址: ...
 - JUnit basic annotation
			
一个标准的Junit 4的运行流程,大致如下:测试类实例化 -> @BeforeClass -> @Before -> @Test -> @After -> @After ...
 - RabbitMQ挂掉问题处理
			
开发环境中的rabbitmq总是会挂掉,rabbitmq的执行都是ssh远程登录执行命令: rabbitmq-server & 认为加了&,进程会在后台执行不会受到终端的影响.所以不知 ...
 - Java自动装箱和自动拆箱操作
			
1.Java数据类型 在介绍Java的自动装箱和拆箱之前,我们先来了解一下Java的基本数据类型. 在Java中,数据类型可以分为两大种,Primitive Type(基本类型)和Reference ...