Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

c++ - 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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...