C ++纯虚拟多重继承? [英] C++ pure virtual multiple inheritance?

查看:116
本文介绍了C ++纯虚拟多重继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来实现一个使用接口的多重继承的实现...

I need help for an implementation that uses multiple inheritance of Interfaces...

现有的代码有一个具有很多功能的接口。实例是使用工厂创建的。

There is an existing code whith an interface which has a lot of functions. The instances are created using a factory.

class IBig
{
    // Lot of pure virtual functions
};

并且他的补充:

class CBig: public IBig
{
    // Implementation
}

我想在多个较小的接口中拆分接口,但它应该与现有代码保持一段时间兼容。

I Want to split the interface in multiple smaller interfaces, but it should stay compatible to the existing code for some time.

这是一个我试图做的样本:

Here is a sample of what I tried to do:

class IBaseA
{
public:
    virtual void DoA() = 0;
};

class IBaseB
{
public:
    virtual void DoB() = 0;
};

// The same interface, now based on multiple smaller interfaces
class IBig : public IBaseA, public IBaseB
{
};

class CBaseA: public IBaseA
{
public:
    virtual void DoA()
    {
        printf("DoA\n");
    }
};

class CBaseB: public IBaseB
{
public:
    virtual void DoB()
    {
        printf("DoB\n");
    }
};

// Inherit from base classes where the implementation is, and from IBig as 
// the instance of CBig is returned as IBig.
class CBig: public CBaseA, public CBaseB, public IBig
{
};

这里的问题是CBig类无法实例化。编译器说DoA和DoB函数是纯虚函数,即使它们是在CBaseA和CBaseB中实现的。如果我不想再次实现这些函数,只需要调用基类的函数,我该怎么办?

The problem here is that the class CBig cannot be instanciated. The compiler says the functions DoA and DoB are pure virtual, even if they are inplemented in CBaseA and CBaseB. What should I do if i don't want to implement again the functions, just to call the function of the base class ?

注意:我知道设计很难看,但这只是暂时的,直到可以更换大界面,并且....我想了解! ; - )

NB: I know the design is ugly, but this is only temporary until the big interface can be replaced, and.... I want to understand ! ;-)

提前致谢!

推荐答案

在这里你应该使用虚拟继承。此功能可确保在实例化子类时,只有一个虚拟继承的基类实例。对于您的示例,这看起来像:

Here you should use virtual inheritance. This feature assures that there is only one instance of your virtually-inherited base class when you instantiate a subclass. For your example, this would look like:

#include <cstdio>

class IBaseA
{
public:
    virtual void DoA() = 0;
};

class IBaseB
{
public:
    virtual void DoB() = 0;
};

// The same interface, now based on multiple smaller interfaces
class IBig : virtual public IBaseA,  virtual public IBaseB
//              ^                       ^
{
};

class CBaseA: virtual public IBaseA
//              ^
{
public:
    virtual void DoA()
    {
        printf("DoA\n");
    }
};

class CBaseB: virtual public IBaseB
//              ^
{
public:
    virtual void DoB()
    {
        printf("DoB\n");
    }
};

// Inherit from base classes where the implementation is, and from IBig as 
// the instance of CBig is returned as IBig.
class CBig: public CBaseA, public CBaseB, public IBig
{
};

int main()
{
    CBig cb;
}

以上更改确保没有额外的 IBaseA 和时创建的> DoA DoB IBaseB 多次。

The above changes ensure that there are not extra declarations of DoA and DoB created when you inherit from IBaseA and IBaseB multiple times.

这篇关于C ++纯虚拟多重继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆