help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Compile errors when virtual inheritance used.


From: Thomas Maeder
Subject: Re: Compile errors when virtual inheritance used.
Date: Wed, 08 Dec 2010 15:42:19 -0000
User-agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux)

Gang Of Meemure <shahan.am@gmail.com> writes:

> When trying to compile this code segment
>
> //-----------------------------------------------------------------------------
> class A
> {
> public:
>     virtual void foo(int, int) {}
> };
>
> class B : public A
> {
> };
>
> class C
> {
> public:
>     virtual void foo(int) {}
> };
>
> class D: virtual public B, public C
> {
> public:
>     /*
>     void foo(int i, int j)
>     {
>         B::f(i, j);
>     }
>     */
> };
>
> class E: public B
> {
> };
>
> class F: public D, virtual public E
> {
>
> public:
>     void foo(int i, int j)
>     {
>         D::foo(i, j);
>     }
> };
>
> //------------------------------------------------------------------------------
>
> Im getting following error
>
> main.cpp: In member function virtual void F::foo(int, int):
> main.cpp:45: error: reference to foo is ambiguous
> main.cpp:18: error: candidates are: virtual void C::foo(int)
> main.cpp:5: error:                 virtual void A::foo(int, int)
> main.cpp:45: error: reference to foo is ambiguous
> main.cpp:18: error: candidates are: virtual void C::foo(int)
> main.cpp:5: error:                 virtual void A::foo(int, int)
>
> How can this be possible ? Clearly function signatures of function foo
> in class A and C are different.

Irrelevant. Name lookup comes before argument type matching.

It's normally not a good idea to mix overloading and overriding.


> Btw if i uncomment the function foo in class D this compiles with
> out an error.

The inheritances structure (which is far too complex for a human to
grasp) can be drawn like this:

A -> B ->  E ->*
                 F
       ->*
           D -> 
     C ->

I.e. D::foo, if declared, hides C::foo and A::foo.

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---


reply via email to

[Prev in Thread] Current Thread [Next in Thread]