Topic: Hiding member function with overload in derived class
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/04/02 Raw View
In article <38E3D429.84158C3B@nwu.edu>, Jeremy Weinberger
<jeremy@nwu.edu> writes
>Why does overloading a function in a derived class, so that it takes
>different arguments that the form in the base class, hide the base-class
>function?
Because that is the only safe way that the overload resolution rules can
work. Name look-up is fearsomely complicated at the best of times.
If you want the base class versions in the overload set for the derived
class a simple using declaration will do.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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: Jeremy Weinberger <jeremy@nwu.edu>
Date: 2000/04/01 Raw View
I am interested in an explanation for the following behavior, with
regard to the c++ standard.
Why does overloading a function in a derived class, so that it takes
different arguments that the form in the base class, hide the base-class
function? I understand that if the derived-class function took the same
arguments (as the base-class form), this would simply be overriding the
base-class function, and as such, calls to this function on
derived-class objects would bind to the derived-class function. However,
if I provide an overloaded form in the derived class, the base-class
form is hidden, and the name is no longer available in the derived
class. Doesn't member function binding rely on both the name and
arguments to this function?
The following sample code demonstrates this behavior:
class Fred {
public:
void foo() {
cout << "Fred's foo\n";
}
};
class George : public Fred {
public:
void foo(int i) {
cout << "George's foo\n";
}
};
int main() {
George g1;
g1.foo(); //This will not compile
//No matching function for George::foo()
return 0;
}
foo() is not available in George, unless you say
using Fred::foo;
OR
void George::foo() {
Fred::foo();
}
in the definition of George.
jeremy
--
-----------------
Jeremy Weinberger jeremy@nwu.edu http://isp.nwu.edu/~jeremy
iCAIR 1890 Maple, Suite 150 Evanston, IL USA
Voice: 847-491-4054 Fax: 847-467-7885 Pager: 847-225-1227
"You have no respect for excessive authority or obsolete
traditions! You're dangerous and depraved and you ought to
be taken outside and shot!"
---
[ 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: Ron Natalie <ron@sensor.com>
Date: 2000/04/01 Raw View
Jeremy Weinberger wrote:
>
>
> Why does overloading a function in a derived class, so that it takes
> different arguments that the form in the base class, hide the base-class
> function?
Because it has nothing to do with the arguments at all. Essentially the
way it finds the appropriate member function to run is that it:
1. Looks up the name.
2. Finds the possible overloads for that name.
3. Determines if the access is allowed.
4. If the function is virtual, find the final overrider.
Object G1 finds George::foo which has one overload expecting int.
If you want to bring Fred::foo up into George's scope add:
using Fred::foo;
to the George definition.
> I understand that if the derived-class function took the same
> arguments (as the base-class form), this would simply be overriding the
> base-class function, and as such, calls to this function on
> derived-class objects would bind to the derived-class function.
As I said it has nothing to do with the arguments.
> However,
> if I provide an overloaded form in the derived class, the base-class
> form is hidden, and the name is no longer available in the derived
> class.
It is always hidden unless specifically exposed. The name foo in Fred
is hidden in George by George's foo.
---
[ 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: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/04/01 Raw View
Jeremy Weinberger wrote:
>
> Why does overloading a function in a derived class, so that it takes
> different arguments that the form in the base class, hide the base-class
> function?
It is a consequence of the fundamental rule of scopes. A name in an
inner scope hides all instances of that name in all outer scopes.
A base class is considered to be in an outer scope relative to a
derived class. Thus, a name declared in a derived class hides all
instances of that name in all of the base classes. It doesn't matter
whether any of those names are functions, much less what arguments the
functions might have.
Overloading occurs only within a given scope. To overload a base-class
function in a derived class, you can define a new function in the
derived class that calls the base-class function, or provide a
using-declaration that brings the base-class name into the scope of
the derived class.
--
Steve Clamage, stephen.clamage@sun.com
---
[ 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 ]