Topic: Overloading functions and multiple inheritance ?
Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 2000/10/16 Raw View
abdel Meghdir wrote:
> The folowing code generate compiler error C2385 with VC++ 5.0 .
> DerBase is dervived from both base1 and base2 . and the function foo()
> is overloaded .
> is this a standard C++ or just an other VC++ bug ?
This is not a VC++ bug. According to the C++ standard, the code is
ill-formed, and so a diagnostic is required.
> class base1
> {
> public:
> virtual void foo() { cout<< "base1::foo"<<endl ; };
> };
>
> class base2
> {
> public :
> void foo(int i) { cout<< "base2::foo"<<endl; };
> };
>
> class DerBase : public base1 , public base2
> {
> };
>
> void main( )
> {
> DerBase obj;
> obj.foo( ); // error C2385: 'DerBase::foo' is ambiguous
> }
Overloading only distinguishes between different names defined in
a single scope, not between names defined in different scopes.
See 10.2 [class.member.lookup].
If you want to allow overloading, you can use a `using' directive
in the derived class:
class DerBase : public base1 , public base2
{
using base1::foo;
using base2::foo;
};
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Andrew J Robb <ajrobb@pavilion.co.uk>
Date: Sun, 15 Oct 2000 03:26:44 GMT Raw View
abdel Meghdir wrote:
> The folowing code generate compiler error C2385 with VC++ 5.0 .
> DerBase is dervived from both base1 and base2 . and the function foo()
> is overloaded .
> is this a standard C++ or just an other VC++ bug ?
>
> class base1
> {
> public:
> virtual void foo() { cout<< "base1::foo"<<endl ; };
> };
>
> class base2
> {
> public :
> void foo(int i) { cout<< "base2::foo"<<endl; };
> };
>
> class DerBase : public base1 , public base2
> {
> };
>
> void main( )
> {
> DerBase obj;
> obj.foo( ); // error C2385: 'DerBase::foo' is ambiguous
> }
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
I have some sympathy for the compiler complaining. If in base2 you had declared
foo with a default argument value, there would be an ambiguity. I suspect that
the default is only used during compilation and not linking.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Anthony Williams" <anthony_w.geo@yahoo.com>
Date: Wed, 13 Sep 2000 02:39:59 GMT Raw View
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
"abdel Meghdir" <kmeghdir@yahoo.com> wrote in message
news:8pldhm$pm0$1@reader1.imaginet.fr...
>
> The folowing code generate compiler error C2385 with VC++ 5.0 .
> DerBase is dervived from both base1 and base2 . and the function
foo()
> is overloaded .
> is this a standard C++ or just an other VC++ bug ?
>
> class base1
> {
> public:
> virtual void foo() { cout<< "base1::foo"<<endl ; };
> };
>
> class base2
> {
> public :
> void foo(int i) { cout<< "base2::foo"<<endl; };
> };
>
> class DerBase : public base1 , public base2
> {
> };
>
> void main( )
>
> DerBase obj;
> obj.foo( ); // error C2385: 'DerBase::foo' is ambiguous
> }
DerBase has two foo functions, base1::foo and base2::foo. Before checking
the
parameters, the compiler must decide which of the two the name refers to,
which
it cannot. You need to explicitly specify which in the call:
obj.base1::foo();
or add both to DerBase:
class DerBase: public base1, public base2
{
void foo(){ base1::foo(); }
void foo(int i) { base2::foo(i); }
};
Anthony
- --
alink@anthonyw.cjb.net -- Questions relating to ALINK
anthony@anthonyw.cjb.net -- Non-ALINK questions
http://anthonyw.cjb.net/ -- ALINK home page
PGP Fingerprint:
0E2D D32A 8732 DC31 804C D435 9BF0 F8FE 1C1B 9AD5
PGP Key at: http://i3.yimg.com/3/c7e5ee24/g/68fc2307.asc
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.1 for non-commercial use <http://www.pgp.com>
Comment: PGP Key at: http://i3.yimg.com/3/c7e5ee24/g/68fc2307.asc
iQA/AwUBOb5RG5vw+P4cG5rVEQI5HgCdGu3hwgX8jd9lH6KORbEYsa1p6pEAnRSt
5FHhytgT3CHp/eZhrGNnFE0f
=xM8O
-----END PGP SIGNATURE-----
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "abdel Meghdir" <kmeghdir@yahoo.com>
Date: Tue, 12 Sep 2000 15:32:47 GMT Raw View
The folowing code generate compiler error C2385 with VC++ 5.0 .
DerBase is dervived from both base1 and base2 . and the function foo()
is overloaded .
is this a standard C++ or just an other VC++ bug ?
class base1
{
public:
virtual void foo() { cout<< "base1::foo"<<endl ; };
};
class base2
{
public :
void foo(int i) { cout<< "base2::foo"<<endl; };
};
class DerBase : public base1 , public base2
{
};
void main( )
{
DerBase obj;
obj.foo( ); // error C2385: 'DerBase::foo' is ambiguous
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]