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:
multiple-diamond-inheritance-compiles-without-virtual-but-doesnt-with
and
why does GCC give me an error - final overrider
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 the 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;
}
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 an implementation on the der3
class it is giving me a compiler error:
error: no unique final override for ‘virtual void base::fun()’ in ‘der3’
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 any way?
Any clue or help is appreciated. I am using g++ 4.6.3.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…