 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ivan Godard Guest
|
Posted: Fri Jun 10, 2005 10:21 am Post subject: language suggestion: "using" to resolve ambiguous overriding |
|
|
Code like:
struct base { virtual void foo() = 0; };
struct d1 : public virtual base { virtual void foo() {} };
struct d2 : public virtual base { virtual void foo() {} };
struct der : public d1, public d2 { };
is an error, getting something like:
foo.cc:4: error: no unique final overrider for `virtual void
base::foo()' in `der'
To resolve the ambiguity you have to provide a local override that
forwards to the intended inherited override:
struct der : public d1, public d2 { virtual void foo() {
d1::foo() };
How about letting a using declaration do the job as well:
struct der : public d1, public d2 { using d1::foo; };
It would break no code and is a natural integration of the existing
inheritance and using notions.
Comments?
Ivan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
ta0kira@yahoo.com Guest
|
Posted: Mon Jun 13, 2005 4:16 pm Post subject: Re: language suggestion: "using" to resolve ambiguous overri |
|
|
What happens when either d1 or d2 doesn't extend the virtualness of
foo? A "final overrider" would cover up one/both of them. I think
it's better to have an ambiguity error because even though there is
only one base::foo in der, there is also d1::foo and d2::foo which
under no circumstances should be automatically resolved. In any case,
I don't think it is necessary if you avoid making classes with virtual
functions virtual base classes; wait to make them virtual until your
final non-virtual definition of the functions, or just leave them
abstract until the multiple instances converge.
ta0kira
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alan McKenney Guest
|
Posted: Mon Jun 13, 2005 7:45 pm Post subject: Re: language suggestion: "using" to resolve ambiguous overri |
|
|
Ivan Godard wrote:
| Quote: | Code like:
struct base { virtual void foo() = 0; };
struct d1 : public virtual base { virtual void foo() {} };
struct d2 : public virtual base { virtual void foo() {} };
struct der : public d1, public d2 { };
is an error, getting something like:
foo.cc:4: error: no unique final overrider for `virtual void
base::foo()' in `der'
To resolve the ambiguity you have to provide a local override that
forwards to the intended inherited override:
struct der : public d1, public d2 { virtual void foo() {
d1::foo() };
How about letting a using declaration do the job as well:
struct der : public d1, public d2 { using d1::foo; };
It would break no code and is a natural integration of the existing
inheritance and using notions.
|
"using" refers to names. Functions are not uniquely defined by
their name, but by their name and signature (hence, name-mangling.)
An example where your idea breaks down:
struct base { void f( int ); void f( float ); ... };
struct d1 { void f( int ); void f( float ); ... };
struct d2 { void f( int ); ... };
struct dd { using d2::f; ... };
dd d;
d.f( 2.5 ); // which f? base::f(float)? d1::f(float)? d2::f(int)?
The standard-conforming method for resolving is a little
clunky, but makes it clear which f() you want, and with
a good compiler, should incur no extra cost.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|