热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

20210921c++继承,虚继承(内存结构)

原标题:2021-09-21c++继承,虚继承(内存结构)普通的公有继承

原标题:2021-09-21c++ 继承,虚继承(内存结构)

普通的公有继承

class test1
{
public:
test1(int i) :num1(i) {}
private:
int num1;
};
class test2 : pwww.yii666.comublic 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 iwww.yii666.com,int j) :test1(i), num2(j)
{

}
private:
int num2;
};
void main()
{
test2 t2(1, 2);
}

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

这四字文章来源地址51281.html节就是我们的虚基类表的地址

跟随虚继承表 其中存储了本类距离父类对象的差值 通过差值文章来源站点https://www.yii666.com/能够找到父类对象

我们再看这个内存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:
i文章来源地址51281.htmlnt 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

来源于:2021-09-21c++ 继承,虚继承(内存结构)


推荐阅读
author-avatar
V体验_数码IT
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有