news 2026/6/13 6:02:52

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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

总结

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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/13 6:02:52

AI学习操作系统:构建可验证、可反馈、可演进的认知网络

1. 项目概述:这是一份写给真实从业者的AI学习通讯,不是资讯聚合,也不是知识搬运“Learn AI Together — Towards AI Community Newsletter #24”这个标题里藏着三个被多数人忽略的关键信号:Learn(动词,强调…

作者头像 李华
网站建设 2026/6/13 5:55:17

2026年6月12日博客精选

今日摘要 今天我们重点关注前沿 AI 技术的工程实践与技术生态的最新演进。从 Claude 模型在日常任务中展现的极强主动性,到 Python 开发者常用的 Datasette 数据工具更新,再到对互联网“劣化时代”的深度反思,这些文章为您呈现了丰富的技术视…

作者头像 李华
网站建设 2026/6/13 5:51:55

Altera FPGA实现的800×480彩条信号源,兼容HV与DE双模式TFT屏驱动

本文还有配套的精品资源,点击获取 简介:专为TFT液晶屏测试设计的FPGA彩条信号发生器,基于Altera平台,输出标准800480分辨率RGB图像。支持行场同步(HV MODE)和数据使能同步(DE MODE&#xff0…

作者头像 李华
网站建设 2026/6/13 5:51:01

多维聚合数据操作:补全、排名、比率与异常检测实战

1. 项目概述:多维聚合中的数据操作,远不止GROUP BY那么简单“Part 20: Data Manipulation in Multi-Dimensional Aggregation”这个标题乍看像教科书某章编号,但实际踩中了数据分析和商业智能工程中最常被低估、最易出错、也最具业务价值的一…

作者头像 李华