Article View: comp.std.c++
Article #2541Re: Overloading of inherited operations?
From: Steve Clamage
Date: Mon, 13 Nov 1995 17:40
Date: Mon, 13 Nov 1995 17:40
49 lines
2113 bytes
2113 bytes
In article 021F9BA0@slip6-2.acs.ohio-state.edu, Sergey Zhupanov <sergey@cis.ohio-state.edu> writes:
>Folks,
>
>Can derived functions overload each other instead of hiding
>or (worse yet) producing ambiguity?
[ examples of functions in different classes in the hierarchy causing
ambiguity instead of overloading ]
>This is the way the language is defined to behave. Unfortunately, this
>single "feature" prevents many interesting idioms from working. (I can
>elaborate if any one is interested.)
>
> ...
>
>Does any one know of a way to make the above code (modified in some way)
>behave in the way intended, as opposed to "the C++ way?"
A using-directive will solve your problem without generating any extra code.
class B1 { public: int foo(int); };
class B2 { public: int foo(char); };
class D : public B1, public B2 {
public:
using B1::foo; // "using" applies to a name, not to a signature
using B2::foo;
double foo(double); // new overload
};
Now all the overloaded "foo" functions in B1 and B2 are added to D's
scope, meaning they overload one another when called from a D object.
D d;
d.foo(1); // calls B1::foo(int)
d.foo('a'); // calls B2::foo(char)
d.foo(1.0); // calls D::foo(double)
Notice how this differs from putting a forwarding function in D. There is no
overhead of any kind with a using-declaration.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<487vm5$ook@engnews1.eng.sun.com>Path:
arc1-ca.pugleaf.net!128.32.155.1!mattReferences:
<01BAB117.021F9BA0@slip6-2.acs.ohio-state.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt From: clamage@Eng.Sun.COM (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Overloading of inherited operations? Date: 13 Nov 1995 17:40:46 GMT Organization: Sun Microsystems Inc. Approved: matt@physics2.Berkeley.EDU (Matt Austern) Message-ID: <487vm5$ook@engnews1.eng.sun.com> References: <01BAB117.021F9BA0@slip6-2.acs.ohio-state.edu> Reply-To: clamage@Eng.Sun.COM NNTP-Posting-Host: physics2.berkeley.edu