当前位置: 首页 > news >正文

C++ 继承,虚继承(内存结构)详解

普通的公有继承

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

classtest1

{

public:

test1(inti) :num1(i) {}

private:

intnum1;

};

classtest2 :publictest1

{

public:

test2(inti,intj) : test1(i), num2(j)

{

}

private:

intnum2;

};

voidmain()

{

test2 t2(1,2);

}

(test2内存结构)查看内存发现父类在子类的上面

多重继承

在原有的代码基础上增加了test3类

test3类继承了 test2和test1

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

classtest1

{

public:

test1(inti) :num1(i) {}

private:

intnum1;

};

classtest2

{

public:

test2(inti) : num2(i)

{

}

private:

intnum2;

};

classtest3 :publictest2 ,publictest1

{

public:

test3(inti,intj,intk) :test1(i), test2(j),num3(k) {}

private:

intnum3;

};

voidmain()

{

test3 t3(1, 2, 3);

}

(test3内存地址 ) 依旧是父类在子类上

但是现在有两个父类为什么test2在test1上?

这和我们的继承顺序有关 我们先继承了test2又继承了test1 更换内存继承顺序 内存的情况也会有所变化

虚继承

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

classtest1

{

public:

test1(inti) :num1(i) {}

private:

intnum1;

};

classtest2:virtualpublictest1

{

public:

test2(inti,intj) :test1(i), num2(j)

{

}

private:

intnum2;

};

voidmain()

{

test2 t2(1, 2);

}

(t2的内存) 我们发现虚继承以后父类成员数据在子类成员数据下面了 首地址处莫名其妙多了四字节

这四字节就是我们的虚基类表的地址

跟随虚继承表 其中存储了本类距离父类对象的差值 通过差值能够找到父类对象

我们再看这个内存0x0082fbd8是t2的首地址 0x0082fbe0是父类的位置

0x0082fbd8 - 0x0082fbe0 == 8

就是本类距离父类对象的差值

虚继承(菱形继承)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

classtest1

{

public:

test1(inti) :num1(i) {}

private:

intnum1;

};

classtest2:virtualpublictest1

{

public:

test2(inti,intj) :test1(i), num2(j)

{

}

private:

intnum2;

};

classtest3 :virtualpublictest1

{

public:

test3(inti,intj) :test1(i), num3(j){}

private:

intnum3;

};

classtest4 :publictest2,publictest3

{

public:

test4(inti,intj,intk) :test1(i),test2(i,j), test3(i,j),num4(k)

{

}

private:

intnum4;

};

voidmain()

{

test4 t4(1, 2,3);

}


test4的内存 我们看到 t2和t3都有自己的虚基类表地址 记录了自己和父类的偏移

两个虚基类表的内容

现在我们计算一下 到爷爷类的差值是否正确

0x00FAFD50 - 0x00fafd3c == 14

0x00FAFD50 - 0x00fafd44 == C

总结

本篇文章就到这里了,希望能够给你带来帮助

http://www.jsqmd.com/news/1003551/

相关文章:

  • CANN昇腾Transformer加速库架构深度解析:融合算子与图算子调度机制如何充分释放昇腾NPU的矩阵算力潜力
  • 2026年 对甲苯磺酸源头厂家推荐榜单:显影剂/医药/塑料/农药原料高纯度对甲基苯磺酸,4-甲苯磺酸生产公司实力解析 - 品牌发掘
  • 2026年服装网站建设公司哪家好,三淼环保服务态度好不好,口碑如何? - myqiye
  • 宝鸡千鸿黄金回收实测盘点与变现攻略 - 余生黄金回收
  • term2048新手入门:从方向键到VI模式的完整操作指南
  • 贝叶斯统计入门误区:从硬币题到业务建模的认知跃迁
  • 从电磁兼容(EMC)倒推PCB设计:你的板子为什么过不了认证?
  • NanaZip完整指南:让Windows文件压缩变得简单高效的终极解决方案
  • 给你的Nginx做个“体检”:用Metricbeat监控QPS、连接数等关键指标(附配置详解)
  • ML模型服务化实战:生产环境稳定性与可观测性设计
  • 2026年高清音视频方案全景分析:潜创微服务商的适配逻辑与选型参考
  • 2026年步进电机选型指南:从技术参数到行业应用的全面解析 - 优质品牌商家
  • 2026年智能酒店用品厨具选购指南:哪家价格实惠口碑好 - 工业品牌热点
  • Python数据科学实战教学包:含航班/乳腺癌/薪资/女性就业等真实数据集与配套课件
  • 牛了,UMG-RAG实现自适应检索粒度
  • 2026年长期办公室保洁托管服务哪家靠谱,性价比高的公司怎么选 - mypinpai
  • 如何让Windows资源管理器直接显示3D模型缩略图
  • PTPX功耗分析避坑指南:毛刺功耗、多轨道模式这些高级选项你真的设对了吗?
  • 微信投票小程序怎么做,2026年最新投票平台深度对比测评 - 投票小程序
  • NSK MCM10010 旗舰级高刚性模组技术指南
  • 保姆级教程:在WinForm项目里给NModbus4 TCP客户端加上“心跳”与重连
  • 2026年仿木混凝土护栏品牌实力评测:从西南到华东,哪些厂家值得关注? - 优质品牌商家
  • 2026年实力强的豆包推广公司排名,靠谱豆包推广公司如何选择 - 工业品牌热点
  • 河南公办大专学历认可度高不高 - myqiye
  • NSK EM5025-6E 高速重载滚珠丝杠技术详解
  • 快递追踪器APP开发实战:基于HarmonyOS API 24的数据驱动应用完整案例
  • 企业级Web宠物商城网站管理系统源码|SpringBoot+Vue+MyBatis架构+MySQL数据库【完整版】
  • LLM通识指南 10|动手搭一个Agent + 通往AGI的三条路
  • 说说725LN销售公司,哪家性价比高 - mypinpai
  • Yokai依赖注入系统详解:基于Fx的现代化应用架构设计