473,387 Members | 1,664 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,387 software developers and data experts.

Why does "function hiding" exists in C++

Hi all,

I have been wondering for a while why function hiding (in a derived
class) exists in C++, e.g. why when writing
class Base {
void foo( int ) {}
};
class Derived: public Base {
void foo( char const [] ) {}
};
the method Base::foo(int) is 'hiden' in the Derived class.
I know that **using** the "using" keyword get reed of function hiding.
But why does hiding itself should be necessary ?

Since I am currently revising my C++ basics, I would really appreciate
if someone could give a good reason / example for the existence of
function hiding, or give me a WWW pointer on this question.

Thx much in advance !
Nicolas

REFERENCES
C++ FAQ LITE / Marshall Cline :
http://www.parashift.com/c++-faq-lit....html#faq-23.9
Unfortunately, this FAQ does not explain the Reason of the behavior...

A thread "reason for existance of function hiding" on a Forum :
http://www.thescripts.com/forum/thread460595.html
Unfortunately this thread does not give good reasons.
May 11 '06 #1
3 6544

Nicolas Castagne wrote:
Hi all,

I have been wondering for a while why function hiding (in a derived
class) exists in C++, e.g. why when writing
class Base {
void foo( int ) {}
};
class Derived: public Base {
void foo( char const [] ) {}
};
the method Base::foo(int) is 'hiden' in the Derived class.


Not sure why the standards committee decided this, maybe one of them
will answer.

My guess is that it allows you to override a function and change the
way it is called - I seem to remember MFC had this with some of the
classes eg I CDialog was derived from CWnd but had a Create() function
that was different. I guess they thought it was a good idea at the
time. In my opinion it is generally flawed logic - if a CDialog really
is a type of CWnd then it should have the same Create function,
although in reality MFC classes are flawed in their use of "is-a" and
"has-a" which causes all sorts of programming difficulties among its
users. (If CWnd is a wrapper for HWND then a CDialog has-a CWnd, i.e.
it has a window, not that it is one. The lifetime of the CDialog class
and its window are different).

May 11 '06 #2
Nicolas Castagne <ca******@imag.fr> writes:
But why does hiding itself should be necessary ?

Puzzled by the same thing I collected some notes into
http://www-h.eng.cam.ac.uk/help/tpl/...++/lookup.html

May 11 '06 #3
Nicolas Castagne wrote:
Hi all,

I have been wondering for a while why function hiding (in a derived
class) exists in C++, e.g. why when writing
class Base {
void foo( int ) {}
};
class Derived: public Base {
void foo( char const [] ) {}
};
the method Base::foo(int) is 'hiden' in the Derived class.


struct base
{
};

struct derived : base
{
void foo(char);
};

derived d;
d.foo(3); // calls derived::foo

Now suppose you get a new version of the library that provides base. The
library writer, of course, knows nothing about your derived class. The
new version of base has a member function named foo that takes an int.
Under the current rules, d.foo(3) still calls derived::foo, which is the
way it ought to be.

--

Pete Becker
Roundhouse Consulting, Ltd.
May 17 '06 #4

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

Similar topics

3
by: crazy_nelly | last post by:
To validate that a function exists on the page, one must simply use the syntax: if ( window.funcName ) //... However, the same syntax does not seem to work when attempting to validate that a...
10
by: Picho | last post by:
Hi all, Lets say I have a "secret" I wish to "hide", lets say a database password. For the more detailed problem, a web application/service that uses a connection string. all the solutions I...
5
by: Amir S. | last post by:
Hi, I'm a newbie to C++ (2 weeks into the course). We were given this assignment to write some code that reads a set of integers (grades) from a file (filename passed by console), outputs them...
15
by: Robert Allan Schwartz | last post by:
I've heard the phrase "Miranda function" used to refer to the 6 member functions supplied by the compiler: default constructor copy constructor destructor operator= operator& const operator&
4
by: Dmitry | last post by:
Hello, Sometimes on js code execution Mozilla 1.5 prints in JS concole the following error: "Error: fn is not defined" where "fn" is the function name surely defined _before_ the line that...
11
by: CWaldman | last post by:
How can I check if a function exists in any of the scripts on my page? I prefer if it can work for both Javascript & VBScript.
10
by: FX | last post by:
I wanna publish a script on my site which allows me to hide image source. i have rough idea abt it. i`ll point src to some php page like: <img src="image.php"> & in tht php wat exactly shud be...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
11
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.