Why does "function hiding" exists in C++

N

Nicolas Castagne

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-lite/strange-inheritance.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.
 
E

Earl Purple

Nicolas said:
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).
 
P

Pete Becker

Nicolas said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top