台北市网站建设_网站建设公司_UI设计_seo优化
2026/1/15 6:28:16 网站建设 项目流程

一、菱形继承:多继承的 “甜蜜陷阱”

1.1 菱形继承的定义与场景

在之前的学习中,我们知道菱形继承是多继承的一种特殊形式,其继承结构呈现菱形拓扑。具体来说,存在一个公共基类(如Person),两个子类(如StudentTeacher)同时继承自该公共基类,最后有一个派生类(如Assistant)同时继承这两个子类。这种结构就像一个菱形,公共基类位于顶端,中间两个子类为菱形的腰,最终派生类为菱形的底边。

1.2 菱形继承引发的两大问题
1.2.1 数据冗余

在普通菱形继承中,最终派生类的对象会包含两份公共基类的成员数据。例如下面的代码中,Assistant对象会同时拥有Student继承而来的_nameTeacher继承而来的_name,这两份数据完全重复,造成了内存浪费。

代码语言:javascript

AI代码解释

class Person { public: string _name; // 姓名 }; // 普通继承,未使用虚继承 class Student : public Person { protected: int _num; // 学号 }; // 普通继承,未使用虚继承 class Teacher : public Person { protected: int _id; // 职工编号 }; // 同时继承Student和Teacher,构成菱形继承 class Assistant : public Student, public Teacher { protected: string _majorCourse; // 主修课程 }; void Test() { Assistant a; // 此时a包含两份_name成员,分别来自Student和Teacher cout << sizeof(a) << endl; // 大小包含两份string和int、int、string }

1.2.2 二义性

由于最终派生类对象中存在两份公共基类的成员,当直接访问该成员时,编译器无法确定访问的是哪一份,从而引发编译错误。

代码语言:javascript

AI代码解释

void Test() { Assistant a; // 编译报错:二义性,无法确定访问的是Student::_name还是Teacher::_name a._name = "peter"; // 可以通过显式指定作用域解决二义性,但数据冗余问题依然存在 a.Student::_name = "xxx"; a.Teacher::_name = "yyy"; }

显式指定作用域虽然能解决编译错误,但并未消除数据冗余,而且在实际开发中频繁使用作用域限定符会增加代码复杂度,降低可读性。因此,菱形继承的这两个问题必须通过更根本的方式解决 ——虚继承

二、虚拟继承:菱形继承的解决方案

2.1 虚继承的语法规则

虚继承的使用非常简单,只需在继承时添加virtual关键字,指定对公共基类的继承为虚继承。需要注意的是,virtual关键字只需在中间子类(如StudentTeacher)继承公共基类(如Person)时添加,最终派生类(如Assistant)继承中间子类时无需添加。

代码语言:javascript

AI代码解释

class Person { public: string _name; // 姓名 }; // 虚继承公共基类Person class Student : virtual public Person { protected: int _num; // 学号 }; // 虚继承公共基类Person class Teacher : virtual public Person { protected: int _id; // 职工编号 }; // 正常继承中间子类,无需再添加virtual class Assistant : public Student, public Teacher { protected: string _majorCourse; // 主修课程 }; void Test() { Assistant a; // 正常访问,无二义性 a._name = "peter"; cout << sizeof(a) << endl; // 大小仅包含一份string,数据冗余问题解决 }

通过虚继承,最终派生类对象中只会保留一份公共基类的成员,既解决了二义性,又消除了数据冗余。但虚继承是如何实现这一效果的呢?其底层内存模型发生了怎样的变化呢?

三、菱形虚拟继承的底层实现原理

要理解虚继承的原理,必须深入分析其内存模型。由于 VS 编译器的监视窗口会对内存模型进行优化显示,无法看到真实的底层结构,因此我们需要借助内存窗口,并通过简化的代码示例进行分析。

3.1 简化的菱形虚拟继承模型

为了便于观察内存布局,我们使用更简单的数据类型(int)替代复杂类型(string),构建简化的菱形虚拟继承体系:

代码语言:javascript

AI代码解释

class A { public: int _a; }; // 虚继承A class B : virtual public A { public: int _b; }; // 虚继承A class C : virtual public A { public: int _c; }; // 继承B和C,构成菱形虚拟继承 class D : public B, public C { public: int _d; }; int main() { D d; d._a = 3; // 公共基类成员 d._b = 4; // B类成员 d._c = 5; // C类成员 d._d = 6; // D类自身成员 return 0; }
3.2 内存模型核心:虚基表与偏移量

通过内存窗口观察D对象的内存布局,会发现其结构与普通菱形继承有显著差异。虚拟继承的核心设计是:中间子类(B、C)不再直接存储公共基类(A)的成员,而是存储一个指向 “虚基表” 的指针,虚基表中存储了当前子类部分到公共基类成员的相对偏移量

www.dongchedi.com/article/7595302803858293310
www.dongchedi.com/article/7595302533728551486
www.dongchedi.com/article/7595303526705152574
www.dongchedi.com/article/7595302533728911934
www.dongchedi.com/article/7595302732916277822
www.dongchedi.com/article/7595300345145164313
www.dongchedi.com/article/7595287730565710361
www.dongchedi.com/article/7595287469617529368
www.dongchedi.com/article/7595289256520663576
www.dongchedi.com/article/7595287092394000920
www.dongchedi.com/article/7595286612611957273
www.dongchedi.com/article/7595285133738410521
www.dongchedi.com/article/7595285905729487384
www.dongchedi.com/article/7595287514580435481
www.dongchedi.com/article/7595285072006382105
www.dongchedi.com/article/7595285719334502936
www.dongchedi.com/article/7595285631619007000
www.dongchedi.com/article/7595277509785453081
www.dongchedi.com/article/7595276630432760345
www.dongchedi.com/article/7595275735409967640
www.dongchedi.com/article/7595277089067549246
www.dongchedi.com/article/7595276413155295769
www.dongchedi.com/article/7595276373905195544
www.dongchedi.com/article/7595274144955499033
www.dongchedi.com/article/7595274423667048984
www.dongchedi.com/article/7595275907217195545
www.dongchedi.com/article/7595274913787953689
www.dongchedi.com/article/7595274806057337368
www.dongchedi.com/article/7595274833727406617
www.dongchedi.com/article/7595255011878208062
www.dongchedi.com/article/7595255232834159166
www.dongchedi.com/article/7595244636982379033
www.dongchedi.com/article/7595246619336000062
www.dongchedi.com/article/7595245102663352894
www.dongchedi.com/article/7595246113737982526
www.dongchedi.com/article/7595238963515146814
www.dongchedi.com/article/7595238605032292888
www.dongchedi.com/article/7595237840809198105
www.dongchedi.com/article/7595237829975212569
www.dongchedi.com/article/7595237024668877336
www.dongchedi.com/article/7594914712018600510
www.dongchedi.com/article/7594913283375907352
www.dongchedi.com/article/7594914016129106456
www.dongchedi.com/article/7594914424213766718
www.dongchedi.com/article/7594913083894891033
www.dongchedi.com/article/7594913119710069310
www.dongchedi.com/article/7594912459706696254
www.dongchedi.com/article/7594911633613390360
www.dongchedi.com/article/7594909036307595800
www.dongchedi.com/article/7594910057444786750
www.dongchedi.com/article/7594909893274927641
www.dongchedi.com/article/7594909974816588350
www.dongchedi.com/article/7594909035217404441
www.dongchedi.com/article/7594908551181615678
www.dongchedi.com/article/7594906883010855486
www.dongchedi.com/article/7594907513641058878
www.dongchedi.com/article/7594905549272285720
www.dongchedi.com/article/7594906861996884505
www.dongchedi.com/article/7594906134906421785
www.dongchedi.com/article/7594905869373096472
www.dongchedi.com/article/7594904114086560281
www.dongchedi.com/article/7594903301414650392
www.dongchedi.com/article/7594901235942228542
www.dongchedi.com/article/7594901805579452953
www.dongchedi.com/article/7594900315275657752
www.dongchedi.com/article/7594899458794799641
www.dongchedi.com/article/7594900266315645465
www.dongchedi.com/article/7594900151853236798
www.dongchedi.com/article/7594898846111580697
www.dongchedi.com/article/7594899021756776984


www.dongchedi.com/article/7594897166766965272
www.dongchedi.com/article/7594897310665245246
www.dongchedi.com/article/7594897192310407705
www.dongchedi.com/article/7594897631617794585
www.dongchedi.com/article/7594896580080435737
www.dongchedi.com/article/7594895704896684568
www.dongchedi.com/article/7594897374330503705
www.dongchedi.com/article/7594895618720449048
www.dongchedi.com/article/7594896034183397912
www.dongchedi.com/article/7594896087656694334

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询