C ++:从基类和具有公共基接口的多个接口继承

时间:2022-09-24 23:58:46

Here is my classes hierarchy (briefly):

这是我的类层次结构(简要):

struct block_i
{
  virtual ~block_i() = default;
  virtual bool is_done() const = 0;
  //...
};
template<class T>
struct consumer_block_i : public block_i
{
  virtual void register_producer(producer_block_i<T>&) = 0;
  //...
};
template<class T>
struct action_block : public consumer_block_i<T>
{
  virtual void register_producer(producer_block_i<T>&) override {}
  //...
};

It works (I believe).

它有效(我相信)。

block_i, consumer_block_i - are pure virtual interfaces. I want to keep theme pure virtual. There multiple user-end blocks (such as action_block or transform_block) and they share a lot of code and I would like to put such common code into base class (block_base for example).

block_i,consumer_block_i - 是纯虚拟接口。我想保持主题纯虚拟。有多个用户端块(例如action_block或transform_block),它们共享大量代码,我想将这些公共代码放入基类(例如block_base)。

Here is what I want in C#:

这是我在C#中想要的:

interface IBlock
{
    bool IsDone();
}

interface IProducer : IBlock
{
    void RegisterConsumer(IConsumer producer);
}

interface IConsumer : IBlock
{
    void RegisterProducer(IProducer producer);
}

class BlockBase : IBlock
{
    public bool IsDone() { return false; }
}

class Consumer : BlockBase, IConsumer
{
    private IProducer producer_;

    public void RegisterProducer(IProducer producer) { producer_ = producer; }
}

In C++ I cannot do exactly like in C#. If I try to inherit from both block_base and consumer_block_i (they have common pure virtual interface) then I got compilation error.

在C ++中,我不能完全像在C#中那样。如果我尝试从block_base和consumer_block_i继承(它们有共同的纯虚拟接口),那么我就遇到了编译错误。

struct block_base : public block_i
{
  virtual bool is_done() const override { return false; }
};

template<class T>
class action_block 
  : public virtual block_base
  , public virtual consumer_block_i<T>
{
  virtual void register_producer(producer_block_i<T>&) override {}
  //...
};

Error C2259 'cppdf::action_block': cannot instantiate abstract class

错误C2259'cppdf :: action_block':无法实例化抽象类

How can I achieve this (I mean use pure virtual interfaces and base class at the same time)? Maybe I need to review my architecture? Thanks.

我怎样才能实现这一点(我的意思是同时使用纯虚拟接口和基类)?也许我需要检查一下我的架构?谢谢。

1 个解决方案

#1


0  

block_i is inherited twice.

block_i被继承两次。

  1. action_block:public block_base and here is_done is implemented.
  2. action_block:public block_base,这里实现了is_done。

  3. action_block:public consumer_block_i and here is_done is not implemented! And the implementation of the other base class is unreachable.
  4. action_block:public consumer_block_i,这里没有实现is_done!并且无法访问其他基类的实现。

#1


0  

block_i is inherited twice.

block_i被继承两次。

  1. action_block:public block_base and here is_done is implemented.
  2. action_block:public block_base,这里实现了is_done。

  3. action_block:public consumer_block_i and here is_done is not implemented! And the implementation of the other base class is unreachable.
  4. action_block:public consumer_block_i,这里没有实现is_done!并且无法访问其他基类的实现。