简体   繁体   中英

C++ implementing inherited abstract functions via inherited functions

SOLVED, problem was something a bit different, sorry

I am trying to implement classes composing tools working over interface like this:

class Interface
{
    bool virtual use() = 0; 
}

class Tool : public virtual Interface
{
    void work() { use(); }
}

class InterfaceImplementation : public virtual Interface
{
    bool use() { return DoSomethingUseful(); }
}

class TopClass : public Tool, public InterfaceImplementation
{
    /* whatever using work() */
}

The problem is that TopClass claims to be abstract. As I see it, Tool is abstract because of Interface, and the abstract functions get inherited, that's fine. Functions defined inside TopClass can now override these abstract functions and make TopClass instanciable (that works if I try), why does this not happen when instead of defining these overriding functions, I just inherit them?

I tried: playing with virtuality of inheritances and the order ancestors (in definition of TopClass)

Just for the record: (practically irrelevant) The TopClass has multiple tools and interface is complex, that's why virtual inheritances. I need to use the same InterfaceImplementation (a big class), for defining multiple TopClass-like classes (each with different tools), so I can't define the abstract functions inside TopClass as mentioned above. Simply put, my design has sense when seen in full :)

Edit: The code above was made up, here is an actual code that wouldn't compile. It is a bit more complex, but the problem is that BuildRank has no overrider in AIKBarnard.

struct KPlugExt
{
    bool virtual plug() = 0; 
};

struct KEvaluator : public virtual KPlugExt
{
    virtual int BuildRank(Card card) = 0;
};

struct KBarnardEvaluator : public virtual KPlugExt
{
    int virtual BuildRank(Card card);
};

struct KBarnardActions : public virtual KPlugExt, public virtual KEvaluator
{
    /* stuff */
};

class AIKBarnard : public KBarnardActions, public KBarnardEvaluator
{
public:
    bool plug() { return false; }
};

In your actual code, no class implements the pure virtual method BuildRankTR . Hence, every class in that hierarchy is considered abstract.

Sorry guys, I now know where the problem was.

As you may see above, implementation of one of my interfaces lacked the original interface as an ancestor, so functions were not actually overriding as they should have. Due to many levels in between these posted classes the mistake got lost in code and compiler was pointing to a wrong place in definitions.

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