简体   繁体   English

C ++通过继承函数实现继承的抽象函数

[英]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. 问题在于TopClass声称是抽象的。 As I see it, Tool is abstract because of Interface, and the abstract functions get inherited, that's fine. 如我所见,由于界面的缘故,Tool是抽象的,并且继承了抽象函数,这很好。 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? 现在,在TopClass中定义的函数可以覆盖这些抽象函数,并使TopClass变得不可思议(如果我尝试可以使用),为什么在不定义这些重写函数而直接继承它们的情况下却没有发生这种情况呢?

I tried: playing with virtuality of inheritances and the order ancestors (in definition of TopClass) 我尝试过:发挥继承性和订单祖先的虚拟性(在TopClass的定义中)

Just for the record: (practically irrelevant) The TopClass has multiple tools and interface is complex, that's why virtual inheritances. 仅作记录:(实际上不相关)TopClass具有多个工具,并且接口很复杂,这就是为什么虚拟继承的原因。 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. 我需要使用相同的InterfaceImplementation(一个大类)来定义多个类似TopClass的类(每个类都使用不同的工具),因此无法如上所述在TopClass内定义抽象函数。 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. 它有点复杂,但是问题是BuildRank在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 . 在您的实际代码中,没有类实现纯虚拟方法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. 由于这些发布的类之间有许多层次,因此错误在代码中迷失了,编译器指向了错误的定义位置。

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

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