C++ 继承,虚继承(内存结构)详解
作者:很酷很爱笑 时间:2023-09-05 21:28:01
普通的公有继承
class test1
{
public:
test1(int i) :num1(i) {}
private:
int num1;
};
class test2 : public test1
{
public:
test2(int i,int j) : test1(i), num2(j)
{
}
private:
int num2;
};
void main()
{
test2 t2(1,2);
}
(test2内存结构)查看内存发现父类在子类的上面
多重继承
在原有的代码基础上增加了test3类
test3类继承了 test2和test1
class test1
{
public:
test1(int i) :num1(i) {}
private:
int num1;
};
class test2
{
public:
test2(int i) : num2(i)
{
}
private:
int num2;
};
class test3 :public test2 ,public test1
{
public:
test3(int i, int j,int k) :test1(i), test2(j),num3(k) {}
private:
int num3;
};
void main()
{
test3 t3(1, 2, 3);
}
(test3内存地址 ) 依旧是父类在子类上
但是现在有两个父类为什么test2在test1上?
这和我们的继承顺序有关 我们先继承了test2又继承了test1 更换内存继承顺序 内存的情况也会有所变化
虚继承
class test1
{
public:
test1(int i) :num1(i) {}
private:
int num1;
};
class test2: virtual public test1
{
public:
test2(int i,int j) :test1(i), num2(j)
{
}
private:
int num2;
};
void main()
{
test2 t2(1, 2);
}
(t2的内存) 我们发现虚继承以后父类成员数据在子类成员数据下面了 首地址处莫名其妙多了四字节
这四字节就是我们的虚基类表的地址
跟随虚继承表 其中存储了本类距离父类对象的差值 通过差值能够找到父类对象
我们再看这个内存0x0082fbd8是t2的首地址 0x0082fbe0是父类的位置
0x0082fbd8 - 0x0082fbe0 == 8
就是本类距离父类对象的差值
虚继承(菱形继承)
class test1
{
public:
test1(int i) :num1(i) {}
private:
int num1;
};
class test2: virtual public test1
{
public:
test2(int i,int j) :test1(i), num2(j)
{
}
private:
int num2;
};
class test3 :virtual public test1
{
public:
test3(int i, int j) :test1(i), num3(j){}
private:
int num3;
};
class test4 :public test2, public test3
{
public:
test4(int i, int j, int k) :test1(i),test2(i,j), test3(i,j),num4(k)
{
}
private:
int num4;
};
void main()
{
test4 t4(1, 2,3);
}
test4的内存 我们看到 t2和t3都有自己的虚基类表地址 记录了自己和父类的偏移
两个虚基类表的内容
现在我们计算一下 到爷爷类的差值是否正确
0x00FAFD50 - 0x00fafd3c == 14
0x00FAFD50 - 0x00fafd44 == C
来源:https://blog.csdn.net/weixin_45012273/article/details/120401539
标签:C++,继承,虚继承,内存结构
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 10 启动分析之init语法详解
2022-03-12 05:07:16
C#通过xpath查找xml指定元素的方法
2023-06-22 11:55:29
C++中cin的返回值问题
2021-07-04 17:59:22
![](https://img.aspxhome.com/file/2023/5/131955_0s.png)
c++虚函数与虚函数表原理
2023-12-09 07:22:45
![](https://img.aspxhome.com/file/2023/6/117756_0s.png)
java输入数字,输出倒序的实例
2023-06-20 01:59:44
![](https://img.aspxhome.com/file/2023/9/91109_0s.jpg)
Java字母加数字组合比较大小
2023-02-27 15:27:52
SpringBoot中使用redis做分布式锁的方法
2023-02-17 04:08:52
![](https://img.aspxhome.com/file/2023/3/82363_0s.png)
C#的WebBrowser操作frame实例解析
2022-03-02 03:46:43
Android开发之BottomSheetDialog组件的使用
2021-12-02 19:01:27
![](https://img.aspxhome.com/file/2023/6/126696_0s.gif)
Qt for Android开发实例教程
2023-06-27 10:00:39
java实现连连看游戏课程设计
2023-10-30 13:18:37
Java实现滑动验证码的示例代码
2022-08-27 15:10:12
![](https://img.aspxhome.com/file/2023/0/74890_0s.jpg)
利用Intellij Idea连接远程服务器实现远程上传部署功能
2022-05-31 13:15:54
![](https://img.aspxhome.com/file/2023/0/62440_0s.png)
JavaScript 与 Java 区别介绍 学java怎么样
2023-11-25 12:23:49
Java事件监听机制讲解
2022-09-18 00:41:11
android studio3.0以上如何通过navicat访问SQLite数据库文件
2022-03-26 14:28:55
![](https://img.aspxhome.com/file/2023/5/89555_0s.png)
hadoop是什么语言
2021-06-18 07:02:38
![](https://img.aspxhome.com/file/2023/0/70970_0s.png)
Android实现自定义圆形进度条
2022-10-28 04:55:22
![](https://img.aspxhome.com/file/2023/2/92602_0s.jpg)
C# 9使用foreach扩展的示例详解
2023-01-27 08:35:56
Java操作excel的三种常见方法实例
2022-12-11 02:29:55
![](https://img.aspxhome.com/file/2023/2/77362_0s.png)