[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: Problem with derived class function name the same as base class
From:       Guillaume Laurent <glaurent () telegraph-road ! org>
Date:       2002-06-29 21:42:30
[Download RAW message or body]

On Sunday 30 June 2002 00:18, Stephen Allewell wrote:
> I would expect this if the functions had identical signatures, but
> where the functions are uniquely defined, I would not have thought it
> a problem for the compiler to use the base class function.

No. This is almost an FAQ, and just about every C++ programmers is bitten by 
it at least once. The rule is that overloading resolution is done within the 
class scope only.

There is actually a very good reason for that, it is to prevent a base class 
from "hijacking" derived class methods when using implicit conversions. For 
example, suppose you have

class Base
{
public:
	void foo(string i);
};

and in a derived class you want a version of foo() taking an int :

class Child : public Base
{
public:
	void foo(float i);
};

Suppose now that in your code you call 

Child myChild;
myChild.foo(5)

This will call Child::foo(float) of course, because it's the best candidate 
for an int argument. It's implicitly converted to a float.

Now suppose that Base changes for some reason (say you upgrade the library 
containing it, you update it yourself, whatever), and now gets a 
Base::foo(int) method. So Base becomes 

class Base
{
public:
	void foo(string i);
	void foo(int i);
};

Now what happens with

Child myChild;
myChild.foo(5)

?

If the scope of overloading resolution wasn't only at the current class level, 
that code would call Base::foo(int) all of a sudden, because it's now a 
better candidate than your Child::foo(float) method. Good luck in finding 
that kind bug.

-- 
					Guillaume.
					http://www.telegraph-road.org
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic