码迷,mamicode.com
首页 > 其他好文 > 详细

虚继承中的内存布局

时间:2017-06-04 21:18:46      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:虚基类   exp   about   pattern   tps   other   att   pre   分享   

直接看例子就好:

#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。具体这里怎么翻译还得查查

技术分享

 

虚继承中的内存布局

标签:虚基类   exp   about   pattern   tps   other   att   pre   分享   

原文地址:http://www.cnblogs.com/Key-Ky/p/6941593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!