[英]C++: inherit from both base class and multiple interfaces with common base interface
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. block_i
和consumer_block_i
是纯虚拟接口。 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). 有多个用户端块(例如
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#. 在C ++中,我不能完全像在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. 如果我尝试从
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.
谢谢。
block_i is inherited twice. block_i被继承两次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.