0

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

Here is what I want in 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.

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

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.

Dmitry Katkevich
  • 883
  • 7
  • 26

1 Answers1

0

block_i is inherited twice.

  1. action_block:public block_base and here is_done is implemented.
  2. action_block:public consumer_block_i and here is_done is not implemented! And the implementation of the other base class is unreachable.
milbrandt
  • 1,438
  • 2
  • 15
  • 20