Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 默认实现不起作用的多重继承-强制始终覆盖默认实现_C++_Inheritance_Multiple Inheritance - Fatal编程技术网

C++ 默认实现不起作用的多重继承-强制始终覆盖默认实现

C++ 默认实现不起作用的多重继承-强制始终覆盖默认实现,c++,inheritance,multiple-inheritance,C++,Inheritance,Multiple Inheritance,我想做什么 我想有一个基本接口和许多子接口扩展这个基本接口 我想要默认接口的默认实现 我希望我的所有子接口实现都扩展默认实现,并且只覆盖它们想要的那些方法 我定义了以下公共接口-我为其开发插件的SDK需要这些接口 // the base interface class DYNAMIC_ATTRIBUTE IMasterProfile : public IVWUnknown { public: virtual Uint16 VCOM_CALLTYPE Get

我想做什么

  • 我想有一个基本接口和许多子接口扩展这个基本接口
  • 我想要默认接口的默认实现
  • 我希望我的所有子接口实现都扩展默认实现,并且只覆盖它们想要的那些方法
我定义了以下公共接口-我为其开发插件的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 { }
}
问题:

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
默认实现移动到头中,一切正常(此外,我还删除了虚拟继承)。我很好奇,如果不将默认实现移到标题中,是否可以解决这个问题…

问题在于模糊性。您必须在
isomoprofile
中实现
GetNodeVersion
。否则,
SomeProfile::isomoprofile::GetNodeVersion
没有定义,它是抽象的。 考虑这个代码:

class IMasterProfile
{
public:
    virtual int GetNodeVersion() = 0;
};

class ISomeProfile : public IMasterProfile
{
public:
    virtual void SwapData() = 0;
    int GetNodeVersion() override { return 2; };
};

class MasterProfile : virtual IMasterProfile
{
public:
 int GetNodeVersion() override { return 3; };
// ...
};

class SomeProfile : public MasterProfile, public virtual ISomeProfile
{
public:
    SomeProfile(){ std::cout<<ISomeProfile::GetNodeVersion();}
    void print()
    {
        std::cout<<GetNodeVersion(); // ERROR: Call to the "GetNodeVersion" is ambiguous
        std::cout<<MasterProfile::GetNodeVersion(); // Call to the GetNodeVersion from MasterProfile
        std::cout<<ISomeProfile::GetNodeVersion(); // Call to the GetNodeVersion from ISomeProfile, without implementing it's virtual method
    }
    void SwapData() override { }
};

如果没有实现,您就无法做到这一点,需要一个定义。

如果您的
ISomeProfile
使用了
公共虚拟IMasterProfile
,这个问题不会消失吗?我也这么认为。但是SDK不允许我这样做。然后它抱怨无法将
IVWUnknown*
转换为
isomoprofile*
,因为虚拟基类的隐式转换。。。