背景:我提供了两种不同技术的虚拟文件结构:FUSE和MTP。由于两个框架都需要不同的接口,因此我创建了两个为这些接口提供服务的基类。 FUSE famework只知道IFuseFile
接口,而MTP框架只知道IMTPFile
接口。这些基类具有纯虚方法,由派生类实现。
问题:直接实现时,编译器得到"request for member IsWriteable is ambiguous"
(参见示例源代码)。
解决方案:在搜索解决方案时,我只找到了钻石图案。但我只有常见的纯虚方法,没有常见的类。对我来说,一个简单的using BASE::method
可以解决问题。
问题:由于之前我只使用using BASE::method
隐藏方法,我无法解释为什么此代码可以解决我的问题。你能解释一下吗?这只是一个GCC错误/功能吗?
示例:
class IFuseFile
{
virtual bool IsWriteable() const = 0;
public:
int HandleReadRequest( struct fuse_data* pData )
{
if( !IsWriteable() ) return -EACCESS;
...
}
}
class IMTPFile
{
virtual bool IsWriteable() const = 0;
public:
int ReadData( const char* pBuffer, int iSize )
{
if( !IsWriteable() ) return -1;
...
}
}
class PcComFile : public IFuseFile, public IMTPFile
{
using IFuseFile::IsWriteable;
using IMTPFile::IsWriteable;
}
class LogFile : public PcComFile
{
bool IsWriteable() const override { return true; }
}
class StatusFile : public PcComFile
{
bool IsWriteable() const override { return true; }
}
答案 0 :(得分:1)
如果您在PcComFile
中没有使用声明,并且尝试通过IsWriteable
引用或指针访问PcComFile
,则编译器不知道您想要哪一个。它没有任何区别并不重要; C ++的名称查找和歧义规则没有考虑到这两个函数都是抽象的。
其中一个方法的使用声明消除了请求的歧义,并且当两个方法都是抽象的时,它与哪个方法无关。有两个使用声明肯定是多余的,实际上我看起来不对。看起来它应该无法编译或再次模糊不清。