简体   繁体   English

继承抽象基接口及其实现给 C2259

[英]Inheriting both abstract base interface and its implementation gives C2259

I have the following condition:我有以下条件:

An absract base class with many pure virtual functions:具有许多纯虚函数的抽象基类:

interface IBase
{
    virtual void foo1() = 0;
    virtual void foo2() = 0;
    virtual void foo3() = 0;
    virtual void foo4() = 0;
    virtual void foo5() = 0;
    //  ...
    virtual void fooN() = 0;
};

Two small interfaces that inherit it:继承它的两个小接口:
Version-A:版本-A:

interface IBaseExt_A :
    public IBase
{
    virtual void foo_A() = 0;
};

Version-B:版本-B:

interface IBaseExt_B :
    public IBase
{
    virtual void foo_B() = 0;
};

I create base class that implements all of the IBase interface functions:我创建了实现所有IBase接口函数的基类:

class CBase :
    public IBase
{
public:
    virtual void foo1() { /* Do something... */}
    virtual void foo2() { /* Do something... */}
    virtual void foo3() { /* Do something... */}
    virtual void foo4() { /* Do something... */}
    virtual void foo5() { /* Do something... */}
    //  ...
    virtual void fooN() { /* Do something... */}
};

Now, I want to implement both derived versions with minimal code .现在,我想用最少的代码实现两个派生版本。
I was hoping to do something like:我希望做这样的事情:

class CBaseExt_A :
    public IBaseExt_A,
    public CBase
{
public:
    virtual void foo_A() { /* Do something... */}
};

Apparently this gives error:显然这给出了错误:
C2259: 'CBaseExt_A': cannot instantiate abstract class...
These errors refer to all IBase interface functions.这些错误涉及所有IBase接口函数。

I know I can solve it the long way by delegating all IBase functions to CBase implementation:我知道我可以通过将所有IBase函数委托给CBase实现来解决这个问题:

class CBaseExt_A :
    public IBaseExt_A,
    public CBase
{
//  IBase implementation:
public:
    virtual void foo1() { CBase::foo1();}
    virtual void foo2() { CBase::foo2();}
    virtual void foo3() { CBase::foo3();}
    virtual void foo4() { CBase::foo4();}
    virtual void foo5() { CBase::foo5();}
    //  ...
    virtual void fooN() { CBase::fooN();}

//  IBaseExt_A implementation:
public:
    virtual void foo_A() { /* At last - do what we came here for...*}
};

But this makes my small class CBaseExt_A become big and complex.但这使得我的小类CBaseExt_A变得庞大而复杂。
Is there a way how to avoid all this manual delegation coding?有没有办法避免所有这些手动委托编码?

Many thanks, PazO非常感谢,帕兹奥

You should use the following code:您应该使用以下代码:

interface IBase
{
    virtual void foo() = 0;
    ......
};

class CBase : virtual public IBase
{
    void foo() { }
    ......
};

interface IBaseExt_A : virtual public IBase
{
    virtual void foo_A() = 0;
};

struct CBaseExt_A : public IBaseExt_A, public CBase
{
    virtual void foo_A() { /* Do something... */ }
};

Note that both places where the IBase class is inherited should be marked as virtual.请注意,继承IBase类的两个地方都应标记为虚拟。

I think you should specify one of the two inheritance to be virtual.我认为你应该指定两个继承之一是虚拟的。

That is a diamond inheritance where那是钻石的传承

IBase is the top level IBase 是顶级的

IBaseExt_A and CBase are the middle level IBaseExt_A 和 CBase 是中间层

CBaseExt_A is the bottom level CBaseExt_A 是底层

so in CBaseExt_A you want to specify from which path implement the top level, I would say that you may want to specify CBase inheritance in IBaseExt_A to be public virtual instead of just public.所以在 CBaseExt_A 中你想指定从哪个路径实现顶级,我想说你可能想在 IBaseExt_A 中指定 CBase 继承是公共虚拟而不是公共虚拟。

class CBaseExt_A :
public IBaseExt_A,
public virtual CBase
{
public:
    virtual void foo_A() { /* Do something... */}
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM