468,771 Members | 1,763 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

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 6236

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by crazy_nelly | last post: by
10 posts views Thread by Picho | last post: by
15 posts views Thread by Robert Allan Schwartz | last post: by
11 posts views Thread by CWaldman | last post: by
33 posts views Thread by Chen shuSheng | last post: by
11 posts views Thread by sofeng | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.