当前位置:网站首页>The principle of virtual inheritance
The principle of virtual inheritance
2022-08-01 00:14:00 【HUAWEI CLOUD】
虚继承的原理
C++How does the compiler solve data redundancy and ambiguity through virtual inheritance??
1.通过监视窗口已经看不到真实的存在,因为监视窗口被编译器处理过
2.建议使用内存窗口来进行查看
Although virtual inheritance for the diamond inheritance,但是也付出了很大的代价, 1.对象模型更复杂了,学习理解成本很高 2.have a certain efficiency impact
虚拟继承解决数据冗余和二义性的原理 为了研究虚拟继承原理,我们给出了一个简化的菱形继承继承体系,再借助内存窗口观察对象成员的模型.
未使用虚继承:
class A{public: int _a;};class B : public A{public: int _b;};class C : public A{public: int _c;};class D : public B, public C{public: int _d;};int main(){ D d; d.B::_a = 1; d.C::_a = 2; d._b = 3; d._c = 4; d._d = 5; return 0;}
未使用虚继承
下图是菱形继承的内存对象成员模型:这里可以看到数据冗余
先继承B,再继承C We can also see, 先继承的在前面,后继承的在后面,
这里就可以看出为什么菱形继承导致了数据冗余和二义性,根本原因就是D类对象当中含有两个_a成员.
使用虚拟继承后
class A{public: int _a;};class B : virtual public A{public: int _b;};class C : virtual public A{public: int _c;};class D : public B, public C{public: int _d;};int main(){ D d; d.B::_a = 1; d.C::_a = 2; d._b = 3; d._c = 4; d._d = 5; return 0;}
其中D类对象当中的_a成员被放到了最后,而在原来存放两个_a成员的位置变成了两个指针,这两个指针叫虚基表指针,它们分别指向一个虚基表. 虚基表中包含两个数据,第一个数据是为多态的虚表预留的存偏移量的位置(这里我们不必关心),第二个数据就是当前类对象位置距离公共虚基类的偏移量. 也就是说,这两个指针经过一系列的计算,最终都可以找到成员_a
注意:The virtual base table stores relative addresses
下图是菱形虚拟继承的内存对象成员模型:这里可以分析出D对象中将A放到的了对象组成的最下面,这个A 同时属于B和C,那么B和C如何去找到公共的A呢?这里是通过了B和C的两个指针,指向的一张表.这两个指 针叫虚基表指针,这两个表叫虚基表.虚基表中存的偏移量.通过偏移量可以找到下面的A.
下面是上面的Person关系菱形虚拟继承的原理解释:
若是将D类对象赋值给B类对象,在这个切片过程中,就需要通过虚基表中的第二个数据找到公共虚基类A的成员,得到切片后该B类对象在内存中仍然保持这种分布情况
子给父 -> 切片
int main(){ D d; d.B::_a = 1; d.C::_a = 2; d._b = 3; d._c = 4; d._d = 5; B b = d;//切片行为 return 0;}
_a对象仍然存储在该B类对象的最后
Diamond inheritance as long as there are common ancestor classes
例如:
边栏推荐
- Web API 介绍和类型
- Binary tree traversal non-recursive program -- using stack to simulate system stack
- 虚继承的原理
- 助力数字政府建设,中科三方构建域名安全保障体系
- Network security - crack WiFi through handshake packets (detailed tutorial)
- Force buckle 2326, 197
- 【FPGA教程案例43】图像案例3——通过verilog实现图像sobel边缘提取,通过MATLAB进行辅助验证
- mysql having的用法
- 命名实体识别-模型:BERT-MRC
- SQL injection Less42 (POST type stack injection)
猜你喜欢
Introduction to the five data types of Redis
2022年最新重庆建筑八大员(电气施工员)模拟题库及答案
清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架
虹科分享|如何用移动目标防御技术防范未知因素
Handwritten a simple web server (B/S architecture)
pycaret source code analysis: download dataset\Lib\site-packages\pycaret\datasets.py
【MATLAB项目实战】LDPC-BP信道编码
Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
C# Rectangle basic usage and picture cutting
[微服务]分布式事务解决方案-Seata
随机推荐
[微服务]分布式事务解决方案-Seata
日常--Kali开启SSH(详细教程)
Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
Interview assault 69: TCP reliable?Why is that?
SVN服务器搭建+SVN客户端+TeamCity集成环境搭建+VS2019开发
开源好用的 流程图绘制工具 drawio
什么是动态规划,什么是背包问题
C# Rectangle基本用法和图片切割
助力数字政府建设,中科三方构建域名安全保障体系
Flutter教程之 01配置环境并运行demo程序 (教程含源码)
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
vim的基本使用-命令模式
WindowInsetsControllerCompat is simple to use
基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
继承和友元,静态成员的关系
IPD流程专业术语
SQL注入 Less42(POST型堆叠注入)
Likou Binary Tree
In 2022, the latest eight Chongqing construction members (electrical construction workers) simulation question bank and answers
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes