当前位置: 首页 > 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.rkmt.cn/news/1515334.html

相关文章:

  • 2026年定制京作红木家具服务哪家可靠,靠谱选购指南 - myqiye
  • CANN昇腾Transformer加速库架构深度解析:融合算子与图算子调度机制如何充分释放昇腾NPU的矩阵算力潜力
  • 2026年 对甲苯磺酸源头厂家推荐榜单:显影剂/医药/塑料/农药原料高纯度对甲基苯磺酸,4-甲苯磺酸生产公司实力解析 - 品牌发掘
  • 贝叶斯统计入门误区:从硬币题到业务建模的认知跃迁
  • term2048新手入门:从方向键到VI模式的完整操作指南
  • Python数据科学实战教学包:含航班/乳腺癌/薪资/女性就业等真实数据集与配套课件
  • 如何让Windows资源管理器直接显示3D模型缩略图
  • 微信投票小程序怎么做,2026年最新投票平台深度对比测评 - 投票小程序
  • NSK MCM10010 旗舰级高刚性模组技术指南
  • 保姆级教程:在WinForm项目里给NModbus4 TCP客户端加上“心跳”与重连
  • 河南公办大专学历认可度高不高 - myqiye
  • 说说725LN销售公司,哪家性价比高 - mypinpai
  • 别再手动改表了!用Liquibase管理数据库版本,5分钟搞定Spring Boot项目集成
  • Python基础教学:指定目录的遍历操作
  • AdS-Teo虫洞中的共形对称性与量子引力效应
  • 如何快速实现Unity高性能滚动列表:终极优化指南
  • 2026年网站定制开发公司靠谱吗,咨询00Cr25Ni20Mo2N尿素钢厂家哪家好 - mypinpai
  • 如何快速备份CSDN博客内容:面向技术博主的完整解决方案
  • Pintr核心功能揭秘:从照片到线条画的5步魔法
  • 从屏幕规格书到DTSI节点:手把手教你为RK3288/RK3399配置一块新MIPI屏
  • 纯自托管开源MLOps能否达到Level 2?金融级落地实践与避坑指南
  • 告别手动点点点:用CANoe的Trace窗口和IG模块高效排查汽车网络问题(实战案例解析)
  • CANN/cann-bench:Exp指数算子PyPTO基准测试
  • 2026毕业季|知网/维普新规后,公认靠谱的论文降重工具全攻略
  • macOS鼠标侧键魔法:三指滑动全局导航的终极免费方案
  • 时间序列三大基石:平稳性、自相关性与白噪声实战解析
  • 如何快速配置GitHub加速插件:面向开发者的完整指南
  • S_Tide工具箱避坑指南:搞定南海潮流椭圆绘制与潮汐预报的那些‘坑’
  • 停用词不是噪音,而是语义杠杆:Python五大库分层调控实战
  • 安全宣教培训PPT怎么做?从内容到设计手把手教你