473,394 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Overriding overloaded functions in base classes



One of the rules I have recommended for our new coding standard is as
follows:

"When overriding a base class virtual function, all overloads of that
base class virtual function should also be overridden. Otherwise, the
overloads of the overridden function in the base class will not be
visible from the derived class."

In other words...

class foo
{
public:
virtual int abc( int def );
virtual int abc( char ghi, int jkl );
};

class bar : public foo
{
public:
int abc( int def ); // << override of first abc in base class
};

I claim that virtual int abc( char ghi, int jkl ) in class foo is now
invisible to class bar and its users, i.e.:

int w;
char x;
int y;
bar z;

w = z.abc( x, y );

.... will not work.

But, I can't find any reference in Stroustrup (or anywhere else, so
far) that supports that claim.

Am I right or wrong?

If I am right, then I have been asked to provide a reference to
something that supports it, so if any of you happen to know of a
reference I can use (preferably to Stroustrup) that would be much
appreciated.

Thanks...
Nov 29 '07 #1
3 1997
Rick wrote:
>
One of the rules I have recommended for our new coding standard is as
follows:

"When overriding a base class virtual function, all overloads of that
base class virtual function should also be overridden. Otherwise, the
overloads of the overridden function in the base class will not be
visible from the derived class."

In other words...

class foo
{
public:
virtual int abc( int def );
virtual int abc( char ghi, int jkl );
};

class bar : public foo
{
public:
int abc( int def ); // << override of first abc in base class
};

I claim that virtual int abc( char ghi, int jkl ) in class foo is now
invisible to class bar and its users, i.e.:

int w;
char x;
int y;
bar z;

w = z.abc( x, y );

... will not work.

But, I can't find any reference in Stroustrup (or anywhere else, so
far) that supports that claim.

Am I right or wrong?

If I am right, then I have been asked to provide a reference to
something that supports it, so if any of you happen to know of a
reference I can use (preferably to Stroustrup) that would be much
appreciated.
See FAQ 23.9.
http://www.parashift.com/c++-faq-lit....html#faq-23.9

Nov 29 '07 #2
On 2007-11-29 16:41:41 -0500, red floyd <no*****@here.dudesaid:
Rick wrote:
>>
One of the rules I have recommended for our new coding standard is as
follows:

"When overriding a base class virtual function, all overloads of that
base class virtual function should also be overridden. Otherwise, the
overloads of the overridden function in the base class will not be
visible from the derived class."

In other words...

class foo
{
public:
virtual int abc( int def );
virtual int abc( char ghi, int jkl );
};

class bar : public foo
{
public:
int abc( int def ); // << override of first abc in base class
};

I claim that virtual int abc( char ghi, int jkl ) in class foo is now
invisible to class bar and its users, i.e.:

int w;
char x;
int y;
bar z;

w = z.abc( x, y );

... will not work.

But, I can't find any reference in Stroustrup (or anywhere else, so
far) that supports that claim.

Am I right or wrong?

If I am right, then I have been asked to provide a reference to
something that supports it, so if any of you happen to know of a
reference I can use (preferably to Stroustrup) that would be much
appreciated.

See FAQ 23.9.
http://www.parashift.com/c++-faq-lit....html#faq-23.9
Ok. But what is the rational behind this? Why should functions in
derived classes hide those in base classes rather than just overloading
them?

I've been told over and over that a good trait of nice clean software
design is the principle of least surprise. I would've expected that if
a function is in the base class, then it should be visible in the
derived classes also.

--

-kira

Nov 30 '07 #3
On 2007-11-29 21:52:25 -0500, "Alf P. Steinbach" <al***@start.nosaid:
* Kira Yamato:
>On 2007-11-29 16:41:41 -0500, red floyd <no*****@here.dudesaid:
>>Rick wrote:

One of the rules I have recommended for our new coding standard is as
follows:

"When overriding a base class virtual function, all overloads of that
base class virtual function should also be overridden. Otherwise, the
overloads of the overridden function in the base class will not be
visible from the derived class."

In other words...

class foo
{
public:
virtual int abc( int def );
virtual int abc( char ghi, int jkl );
};

class bar : public foo
{
public:
int abc( int def ); // << override of first abc in base class
};

I claim that virtual int abc( char ghi, int jkl ) in class foo is now
invisible to class bar and its users, i.e.:

int w;
char x;
int y;
bar z;

w = z.abc( x, y );

... will not work.

But, I can't find any reference in Stroustrup (or anywhere else, so
far) that supports that claim.

Am I right or wrong?

If I am right, then I have been asked to provide a reference to
something that supports it, so if any of you happen to know of a
reference I can use (preferably to Stroustrup) that would be much
appreciated.
See FAQ 23.9.
http://www.parashift.com/c++-faq-lit....html#faq-23.9

Ok. But what is the rational behind this? Why should functions in
derived classes hide those in base classes rather than just overloading
them?

I've been told over and over that a good trait of nice clean software
design is the principle of least surprise. I would've expected that if
a function is in the base class, then it should be visible in the
derived classes also.

The principle of least surprise works both ways.

You're envisioning an existing base class, someone deriving a new
class, and getting surprised.

Envision instead an existing derived class, someone modifying the base
class (by adding a new overload), and the derived class' programmerer
being surprised that his code now suddenly gives incorrect results.

Granted, that's less likely, but on the other hand it's more serious
because it's not the one modifying the code who is surprised, and the
change is silent.

So of these two possible surprises the current rules try to minimize
the second kind, the "fragile base class", that your derived class code
that depends on a given overload resolution should not be affected by
additions of simple overloads to a base class (unless you ask for it,
in which case that's your decision).
Hmm... Very subtle. C++ truly has its subtleties. Designing a
language with advanced features isn't really an easy undertaking.

--

-kira

Nov 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Generic Usenet Account | last post by:
The code example below fails to compile under g++ and Solaris native CC and gives the following output: "main.cc", line 12: Warning: derived::func hides the virtual function base::func(int)....
8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
1
by: BDob | last post by:
Standard documents "Access control is not considered in determining overriding." To further explain what's puzzling me, I put up an example below: class Base { public: virtual void...
3
by: David Chandler | last post by:
Let me try to describe the situation as clearly as I can. In namespace XXX I have a class PARENT with the following public functions: virtual void foo( const char* stringBuf ); // set...
1
by: Xiangliang Meng | last post by:
Hi, all. When reading C++ books, I'm alway confused by those terms "redefining functions", "overloading functions" and "overriding functions". Please give me some comments on those terms....
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
2
by: Michael | last post by:
Hi All, I have the following: #include <iostream> class BaseClass { protected: int var; public:
4
by: James Emil Avery | last post by:
Hi all, I have a problem where 1. I have many derived classes of a base class. 2. The base class has many functions implementing common behaviour. 3. The code should be the same for all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.