1

问题:
C++ 中是否允许一个类继承自多个父类?

C++ 中的多重继承

  • C++ 支持编写多重继承的代码

    • 一个子类可以拥有多个父类
    • 子类拥有所有父类的成员变量
    • 子类继承所有父类的成员函数
    • 子类对象可以当作任意父类对象使用

  • 多重继承的语法规则
class Derived : public Base A, public Base B, public Base C
{
    // ......
}

多重继承问题 一

编程实验: 多重继承问题 一

#include <iostream>

using namespace std;

class BaseA
{
private:
    int ma;
public:
    BaseA(int a)
    {
        ma = a;
    }
    int getA()
    {
        return ma;
    }
};

class BaseB
{
private:
    int mb;
public:
    BaseB(int b)
    {
        mb = b;
    }
    int getB()
    {
        return mb;
    }
};


class Derived : public BaseA, public BaseB
{
private:
    int mc;
public:
    Derived(int a, int b, int c) : BaseA(a), BaseB(b)
    {
        mc = c;
    }
    int getC()
    {
        return mc;
    }
    void print()
    {
        cout << "ma = " << getA() << ","
             << "mb = " << getB() << ","
             << "mc = " << getC() << endl;
    }
};

int main()
{
    cout << "sizeof(Derived) = " << sizeof(Derived) << endl;  // 注意这里!
    
    Derived d(1, 2, 3);
    
    d.print();
    
    cout << "d.getA() = " << d.getA() << endl; 
    cout << "d.getB() = " << d.getB() << endl; 
    cout << "d.getC() = " << d.getC() << endl; 
    
    cout << endl;
    
    //---------------------------------
    BaseA* pa = &d;
    BaseB* pb = &d;
    
    cout << "pa->getA() = " << pa->getA() << endl;
    cout << "pb->getB() = " << pb->getB() << endl;
    
    cout << endl;
    
    void* paa = pa;
    void* pbb = pb;
    
    if( paa == pbb )                       // 注意这里!!!
    {
        cout << "Pointer to ths same object!" << endl;
    }
    else
    {
        cout << "Error" << endl;
    }
    
    cout << "pa = " << pa << endl;         // 注意这里!!!
    cout << "pb = " << pb << endl;
    cout << "paa = " << paa << endl;
    cout << "pbb = " << pbb << endl;

    return 0;
}
输出:
sizeof(Derived) = 12         ; 子类拥有所有父类成员变量
ma = 1,mb = 2,mc = 3         ; 子类继承父类所有成员函数
d.getA() = 1
d.getB() = 2
d.getC() = 3

pa->getA() = 1               ; 赋值兼用
pb->getB() = 2

Error                        ; 注意这里!!!
pa = 0xbfd98c64
pb = 0xbfd98c68
paa = 0xbfd98c64
pbb = 0xbfd98c68

分析:
BaseA* pa = &d;
BaseB* pb = &d;
BaseA、BaseB 都为 d 的父类,pa,pb 同时指向一个对象,为什么会有不同的地址输出呢?

通过多重继承得到的对象可能拥有”不同的地址“!!

解决方案: 无

void code()
{
    Derived d(1, 2, 3);
    Base* pa = &d;
    Base* pb = &d;
}

clipboard.png

在实际工程开发中,我们往往会通过判断两指针保存的地址值是否相等判断两指针是否指向同一对象。但多继承与我们平常遵循的原则是违背的。
这将极大的增加开发复杂!!!!

多继承的问题 二

clipboard.png

编程实验: 多重继承问题 二

#include <iostream>
#include <string>

using namespace std;

class People
{
private:
    string m_name;
    int m_age;
public:
    People(string name, int age)
    {
        m_name = name;
        m_age = age;
    }
    void print()
    {
        cout << "Name = " << m_name << ","
             << "Age = " << m_age << endl;
    }
};

class Teacher : public People
{
public:
    Teacher(string name, int age) : People(name, age)
    {
    }
};

class Student : public People
{
public:
    Student(string name, int age) : People(name, age)
    {
    }
};

class Doctor : public Teacher, public Student
{
public:
    Doctor(string name, int age) : Teacher(name + "_1", age + 1), Student(name + "_2", age + 2)
    {
    }
};

int main()
{
    Doctor d("D.T", 4);
    
    // d.print();  // Error
    d.Teacher::print();        // 注意这里!
    d.Student::print();        // 注意这里!

    return 0;
}
输出:
Name = D.T_1,Age = 5
Name = D.T_2,Age = 6

当多重继承关系出现闭合时将产生数据冗余的问题!!!

解决方案: 虚继承

class People
{ };

class Teacher : virtual public Pelple 
{ 
};

class Student : virtual public People
{
};

class Doctor : public Teacher, public Student
{
}
  • 虚继承能够解决数据冗余问题
  • 中间层父类不再关心顶层父类的初始化
  • 最终子类必须直接调用顶层父类的构造函数

编程实验: 虚继承初探

#include <iostream>
#include <string>

using namespace std;

class People
{
private:
    string m_name;
    int m_age;
public:
    People(string name, int age)
    {
        m_name = name;
        m_age = age;
    }
    void print()
    {
        cout << "Name = " << m_name << ","
             << "Age = " << m_age << endl;
    }
};

class Teacher : virtual public People
{
public:
    Teacher(string name, int age) : People(name, age)
    {
    }
};

class Student : virtual public People
{
public:
    Student(string name, int age) : People(name, age)
    {
    }
};

class Doctor : public Teacher, public Student
{
public:
    Doctor(string name, int age) : Teacher(name + "_1", age + 1), Student(name + "_2", age + 2), People(name, age)
    {
    }
};

int main()
{
    Doctor d("D.T", 4);
    
    d.Teacher::print();       // 注意这里!
    d.Student::print();
    d.print();

    return 0;
}
输出:
Name = D.T,Age = 4
Name = D.T,Age = 4
Name = D.T,Age = 4

问题:
当架构设计中需要继承时,无法确定使用直接继承还是虚继承
虚继承虽然在技术上解决了数据冗余的问题,但相比单继承清晰的使用规则来说,极大的增加了开发复杂度,尤其在大型软件项目中。

小结

  • C++ 支持多重继承的编程方式
  • 多重继承容易带来问题

    • 可能出现 ”同一对象的地址不同“ 的情况
    • 虚继承可以解决数据冗余的问题
    • 虚继承使得架构设计或代码实现可能出现问题

以上内容参考狄泰软件学院系列课程,请大家保护原创!


TianSong
737 声望139 粉丝

阿里山神木的种子在3000年前已经埋下,今天不过是看到当年注定的结果,为了未来的自己,今天就埋下一颗好种子吧