首页 > 代码库 > 虚继承中的内存布局
虚继承中的内存布局
直接看例子就好:
#include "qdatetime.h" class Person { public: Person(QString name, QDate birthdate) QObject(name.ascii()), m_Birthdate(birthdate) {} Person(const Person& p) : QObject(p), m_Birthdate(p.m_Birthdate) {} private: QDate m_Birthdate; }; class Student : virtual public Person { 1 // other class members }; class Teacher : virtual public Person { 2 // other class members } class GraduateTeachingFellow : public Student, public Teacher { 3 public: GraduateTeachingFellow(const Person& p, const Student& s, const Teacher& t): Person(p), Students(s), Teacher(t) {} 4
Note keyword virtual here. |
|
virtual inheritance. |
|
virtual not needed here. |
|
It is necessary to initialize all virtual base classes explicitly in multiply-derived classes, to resolve ambiguity about how they should be initialized. |
Each instance of a class that virtually inherits from another has a pointer (or a variable offset) to its virtual
base class subobject. 看来这里每个虚继承类的实例代表的是Student和Teacher都会有一个指针指向虚基类的 virtual
base class subobject。具体这里怎么翻译还得查查
虚继承中的内存布局
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。