25

虚继承解决纯虚接口的多次实现问题

 3 years ago
source link: http://pkxpp.github.io/2020/07/22/虚继承解决纯虚接口的多次实现问题/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[TOC]

自己这里在写接口设计的时候,遇到一个问题。类结构设计如下: RRZRR3f.png!web

一开始是普通继承,发现TestInheritedB需要把接口ITestBase里面的纯虚接口都要实现一遍,即使TestInheritedA中已经实现了一遍。翻了一下《深度探索c++对象模型》,改成了虚继承就可以了

普通继承

代码如下:

class ITestBase
{
public:
    virtual void FuncBase() = 0;
};

class ITestInheritedBase : public ITestBase
{
public:
    virtual void FuncInheritedBase() = 0;
};


class TestInheritedA : public ITestBase
{
public:
    TestInheritedA();
    virtual ~TestInheritedA();

    virtual void FuncBase()
    {
        cout << "TestInheritedA::FuncBase" << endl;
    };
};


class TestInheritedB : public ITestInheritedBase, public TestInheritedA
{
public:
    TestInheritedB();
    virtual ~TestInheritedB();

    // 不实现的话报错error C2259: “TestInheritedB”: 不能实例化抽象类
    //virtual void FuncBase()
    //{
    //    cout << "TestInheritedB::FuncBase" << endl;
    //}

    virtual void FuncInheritedBase()
    {
        cout << "TestInheritedB::FuncInheritedBase" << endl;
    };
};

报错信息如下:

error C2259: “TestInheritedB”: 不能实例化抽象类

虚继承

代码如下:

class ITestBase
{
public:
    virtual void FuncBase() = 0;
};

class ITestInheritedBase : virtual public ITestBase
{
public:
    virtual void FuncInheritedBase() = 0;
};


class TestInheritedA : virtual public ITestBase
{
public:
    TestInheritedA();
    virtual ~TestInheritedA();

    virtual void FuncBase()
    {
        cout << "TestInheritedA::FuncBase" << endl;
    };
};


class TestInheritedB : public ITestInheritedBase, public TestInheritedA
{
public:
    TestInheritedB();
    virtual ~TestInheritedB();

    // 不实现的话,也不会报错
    //virtual void FuncBase()
    //{
    //    cout << "TestInheritedB::FuncBase" << endl;
    //}

    virtual void FuncInheritedBase()
    {
        cout << "TestInheritedB::FuncInheritedBase" << endl;
    };
};

类图

yqYJrir.png!web

对象模型

问题解决的同时,也复习了下C++对象模型也还是挺有意思的。借用参考[2]中的一张虚菱形继承的图片。主要思想就是把共有的父类放在了最后,而且保证只有一个,解决了虚继承的二义性。

参考

[1] 《深度探索C++对象模型》 [2] 图说C++对象模型:对象内存布局详解


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK