简体   繁体   中英

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:

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

Version-B:

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

I create base class that implements all of the IBase interface functions:

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.

I know I can solve it the long way by delegating all IBase functions to CBase implementation:

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.
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.

I think you should specify one of the two inheritance to be virtual.

That is a diamond inheritance where

IBase is the top level

IBaseExt_A and CBase are the middle level

CBaseExt_A is the bottom level

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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