当前位置:网站首页>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
例如:

边栏推荐
- 开源好用的 流程图绘制工具 drawio
- Network security - crack WiFi through handshake packets (detailed tutorial)
- vim的基本使用-命令模式
- SQL injection Less47 (error injection) and Less49 (time blind injection)
- Interview Blitz 69: Is TCP Reliable?Why?
- Difference between first and take(1) operators in NgRx
- [MATLAB project combat] LDPC-BP channel coding
- lua入门案例实战123DIY
- Likou Binary Tree
- SVN服务器搭建+SVN客户端+TeamCity集成环境搭建+VS2019开发
猜你喜欢

《ArchSummit:时代的呐喊,技术人听得到》

推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】

类和对象:中

leetcode:126. 单词接龙 II

【MATLAB项目实战】LDPC-BP信道编码

Network security - crack WiFi through handshake packets (detailed tutorial)

力扣二叉树

Matlab / ArcGIS 处理GPM全球月均降水数据

Design of Fire and Anti-theft System Based on Single Chip GSM

谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报
随机推荐
Flink 1.13(八)CDC
Weekly Summary
(26) About menu of the top menu of Blender source code analysis
NIO programming
The role of /etc/resolv.conf
虹科分享|如何用移动目标防御技术防范未知因素
2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
vim的基本使用-底行模式
面试突击69:TCP 可靠吗?为什么?
面试题:实现死锁
Binary tree traversal non-recursive program -- using stack to simulate system stack
TFC CTF 2022 WEB Diamand WriteUp
《ArchSummit:时代的呐喊,技术人听得到》
博弈论(Depu)与孙子兵法(42/100)
Flutter教程之 01配置环境并运行demo程序 (教程含源码)
Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
[AMEX] LGBM Optuna美国运通信用卡欺诈赛 kaggle
谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报