虚拟继承:错误:没有唯一的最终覆盖 [英] Virtual Inheritance: Error: no unique final overrider
问题描述
我知道虚拟继承在这之前和之前提到这个问题我经历了虚拟继承的细节,并通过类似的问题,如下所示:
I know virtual inheritance is covered here before and before asking this question I went through the detail of the virtual inheritance and went through the details of a similar problem like the followings:
多钻石继承编译无虚拟-but-doesnt-with
and
我的问题稍有不同,因为我不使用纯虚拟函数并明确使用虚拟继承来拥有一个唯一的Base类。层次结构如下:
My problem is slightly different as I am not using pure virtual function and explicitly using virtual inheritance to have one unique 'Base' class. The hierarchy is as follows:
Base
/\
/ \
Der1 Der2
\ /
Der3
我知道有关可怕的钻石问题,这就是为什么我使用虚拟继承。
I know about the dreadful diamond on derivation issue and that's why I am using virtual inheritance.
#include <iostream>
class base
{
public :
base()
{
std::cout<<"base()" << std::endl;
}
virtual void fun()
{
std::cout<<"base" << std::endl;
}
};
class der1: virtual public base
{
public :
void fun()
{
std::cout<<"der1" << std::endl;
}
};
class der2 : virtual public base
{
public :
void fun()
{
std::cout<<"der2" << std::endl;
}
};
class der3 : public der1,public der2
{
public :
/*void fun()
{
std::cout<<"der3" << std::endl;
}*/
//if I took out the comment and the function
//is defined it compiles fine as expected
};
int main()
{
base *p=new der3;
//used scope operation explicitly to avoid ambiguity
p->base::fun(); //here it complains about 'no unique final overrider for fun'
return 0;
}
我的理解是,因为我使用虚拟继承,应该只有一个实例'base'和使用范围操作符我可以调用没有歧义的虚拟fun函数。该函数不是纯虚函数。如果我离开和实现在'der3'类它给我编译器错误:
My understanding is since I am using virtual inheritance there should only be one instance of the 'base' and using the scope operator I can invoke without ambiguity the virtual fun function. The function is not pure virtual. If I do leave and implementation on the 'der3' class it is giving me compiler error:
错误:没有唯一的最终重写的虚拟void base :: fun()'in'der3'
error: no unique final overrider for ‘virtual void base::fun()’ in ‘der3’
我可以看到这个问题的工作原理(最终超越)。但我的没有。它在 Base :: fun
, der1 :: fun
和 der2 :: fun
?范围操作员是否帮助?
I can see how this issue works (final overrider). But mine doesn't. Is it getting confused between Base::fun
, der1::fun
and der2::fun
? Does the scope operator help in anyway?
任何线索或帮助是值得赞赏的。我使用g ++ 4.6.3。
Any clue or help is appreciated. I am using g++ 4.6.3.
推荐答案
大多数派生类必须提供虚拟函数的实现基类 - 否则如何提供基类接口,给定中间类(即你的 der1
和 der2
)提供两个选择已经 - 哪一个应该调用?你必须消除歧义(即 der3 :: fun()
)。
The most-derived class has to provide an implementation of the virtual functions in the virtual base class - otherwise how would it provide that base class interface, given the intermediate classes (i.e. your der1
and der2
) provide two alternatives already - which one should it call? You have to disambiguate the situation (i.e. with der3::fun()
).
实际上调用 der3 :: fun()
,因为你明确要求 base :: fun()
这意味着规则不适用,只要你想象你可以实例化一个抽象类,如果你不尝试调用纯虚函数....程序是错误的,直到代码绑定这些松散结束。
Sure you're not actually calling der3::fun()
as you're explicitly requesting base::fun()
, but that doesn't mean the rules don't apply, any more than thinking you could instantiate an abstract class if you don't try to call the pure-virtual functions.... The program is ill-formed until the code ties off these loose ends.
这篇关于虚拟继承:错误:没有唯一的最终覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!