两个接口,多个继承合并到一个容器中? [英] two interfaces, multiple inheritance combine into one container?

查看:132
本文介绍了两个接口,多个继承合并到一个容器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了以下问题:我有两个软件包A和B都可以正常工作.每个都有自己的接口和自己的实现.现在,我制作了一个包C,将A的适配器与B的具体实现组合在一起.C实际上仅实现A的接口,并且目前仅在内部继承和使用B的接口.在大多数情况下,足以从容器访问接口A,但是现在我也需要从B访问方法.这是简单的示例:

I stumbled upon the following problem: I have two packages A and B working fine for each own. Each has its own interface and his own implementation. Now i made a package C combining a adapter of A with a concrete Implemenation of B. C actually only implements the Interface of A and is only inheritating and using the Interface of B internally for now. Most of the times that was enough to have only access to the Interface A from a Container, but now i need the methods from B accessible too. Here is the simple example:

//----Package A----
class IA 
{virtual void foo() = 0;}; 
// I cant add simply bar() here, it would make totally no sense here...

class A : public IA
{virtual void foo() {doBasicWork();} };

//----Package B----
class IB
{virtual void bar() = 0;};

class B1 : public IB
{
    //Some special implementation
    virtual void bar() {} 
};

class B2 : public IB
{
    //Some special implementation
    virtual void bar() {} 
};
// + several additional B classes  , with each a different implementation of bar()

//---- Mixed Classes
class AB1 : public B1, public A
{
void foo() {A::foo(); B1::bar();}
};

class AB2 : public B2, public A
{
void foo() {A::foo(); B2::bar();}
};

// One Container to rule them all: 
std::vector<IA*> aVec;
AB1 obj1;
AB2 obj2;

int main(){
    iAvector.push_back(&obj1);
    iAvector.push_back(&obj2);
    for (std::vector<IA>::iterator it = aVec.begin(); it != aVec.end(); it++)
    {
        it->for(); // That one is okay, works fine so far, but i want also :
//      it->bar(); // This one is not accessible because the interface IA 
                           // doesnt know it.
    }
    return 0;
}

/* I thought about this solution: to inherit from IAB instead of A for the mixed 
   classes, but it doesnt compile, 
stating "the following virtual functions are pure within AB1: virtual void IB::bar()"
which is inherited through B1 though, and i cant figure out where to add the virtual
inheritence. Example:

class IAB : public A, public IB
{
//  virtual void foo () = 0; // I actually dont need them to be declared here again,
//  virtual void bar () = 0; // do i? 

};

class AB1 : public B1, public IAB
{
    void foo() {A::foo(); B1::bar();}
};
*/

问题是,如何实现包A和包B的组合,以便可以从一个容器访问两个接口,同时仍继承A和B的所有实现细节?

The question is, how to achieve a combination of both Packages A and B, so that both Interfaces are accessible from one Container, while all the implementation details from A and B still get inherited?

推荐答案

显而易见的解决方案是创建一个组合接口:

The obvious solution is to create a combined interface:

class IAB : public virtual IA, public virtual IB
{
};

,请从中派生您的AB1AB2(除了它们的 当前导数),并将IAB*保留在向量中.

, have your AB1 and AB2 derive from it (in addition to their current derivations), and keep IAB* in the vector.

这意味着B1B2也必须实际上源自 IB;给定事物似乎前进的方向,A应该 实际上也可能源自IA.

This means that B1 and B2 must also derive virtually from IB; given the direction things seem to be going, A should probably also derive virtually from IA.

有很强的论点表明接口的继承 应该始终是虚拟的.不用走那么远:如果一个班级是 被设计为从衍生而来 应该是虚拟的(并且可以说,如果一个类的设计目的不是为了 衍生自,则不应衍生自).就你而言 您使用的是经典的混合技术,通常, 最简单的解决方案是将mixin中的所有继承 虚拟的.

There are strong arguments that inheritance of an interface should always be virtual. Without going that far: if a class is designed to be derived from, and it has bases, those bases should be virtual (and arguably, if a class is not designed to be derived from, you shouldn't derive from it). In your case, you're using the classic mixin technique, and generally, the simplest solution is for all inheritance in a mixin to be virtual.

这篇关于两个接口,多个继承合并到一个容器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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