【发布时间】:2018-04-27 07:19:49
【问题描述】:
我想做的事
- 我想要一个基础接口和很多扩展这个基础接口的子接口
- 我想要默认接口的默认实现
- 我希望我的所有子接口实现都扩展默认实现,并且只覆盖他们想要的那些方法
我已经定义了以下公共接口 - 我为其开发插件的 SDK 需要这些接口
// the base interface
class DYNAMIC_ATTRIBUTE IMasterProfile : public IVWUnknown
{
public:
virtual Uint16 VCOM_CALLTYPE GetNodeVersion() = 0;
// ...
}
// one of many sub interfaces extending the default one
class DYNAMIC_ATTRIBUTE ISomeProfile : public IMasterProfile
{
public:
virtual void VCOM_CALLTYPE SwapData() = 0;
};
我的实现如下所示:
class DYNAMIC_ATTRIBUTE MasterProfile : public virtual IMasterProfile
{
public:
Uint16 VCOM_CALLTYPE GetNodeVersion() override { return 0; };
// ...
}
class DYNAMIC_ATTRIBUTE SomeProfile : public MasterProfile, public virtual ISomeProfile
{
public:
void VCOM_CALLTYPE SwapData() override { }
}
问题:
编译器抱怨SomeProfile 是抽象的并且没有实现GetNodeVersion 函数。我该如何解决这个问题? SomeProfile 正在扩展 MasterProfile 并且这个类正在实现 GetNodeVersion 函数...
编辑:可能的解决方案
我可以将IMasterProfile 默认实现移动到标题中,一切正常(另外我删除了虚拟继承)。我很好奇这是否可以在不将默认实现移动到标题中的情况下解决...
【问题讨论】:
-
如果您的
ISomeProfile使用public virtual IMasterProfile,这个问题不会消失吗? -
我也这么认为。但是 SDK 不允许我这样做。然后它抱怨它无法将
IVWUnknown*转换为ISomeProfile*,因为隐式转换了虚拟基类...
标签: c++ inheritance multiple-inheritance