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

reason for existance of function hiding

bob
Hi,

I know there exists a good reason why the designers of c++ decided that
function hiding should exist. But I don't know why. Can anybody provide
a good reason/example of a case where function hiding saves the day. I
know there exists one, I'd just like to hear about it.
thanks and have a nice day.

G

Feb 23 '06 #1
9 4958
bo*@blah.com wrote:
Hi,

I know there exists a good reason why the designers of c++ decided that
function hiding should exist. But I don't know why. Can anybody provide
a good reason/example of a case where function hiding saves the day. I
know there exists one, I'd just like to hear about it.
thanks and have a nice day.

G

It's called OOA/OOD where users of an object only have access to its
interfaces. How the internal functioning of that object works should be
of no interest/business of theirs. Hence the collection of functions
that are used to facilitate the objects behaviour are hidden. Think
about driving a car, you only need to know how to use the controls and
not every detail of how it works. It's the same for Computers, Radios,
Televisions, Microwave ovens, and countless other every day things you
might use. Now, do you know anyone, or do you think you might be able to
understand yourself, how all of them work? I'm guessing not.

JB
Feb 23 '06 #2
* bo*@blah.com:

I know there exists a good reason why the designers of c++ decided that
function hiding should exist. But I don't know why. Can anybody provide
a good reason/example of a case where function hiding saves the day. I
know there exists one, I'd just like to hear about it.


IMO there is no convincing reason. However, there is a reason. Namely
the "fragile base class" problem, in this case that the mere addition of
a new member function in a base class should not by default affect
client code using a member function of that name from a derived class,
as it could do without the hiding (think overload resolution).

I find this reason less than convincing because (1) the hiding does not
help when the derived class offers no member function of that name, (2)
the hiding does not help with the "fragile base class" problem in
general, merely a subtle aspect that is not much of a practical problem,
and (3) the hiding is very counter-intuitive to most programmers (an
extreme case is where an automatically generated assignment operator in
a derived class hides a custom assignment operator in a base class), and
forces us to write extra, silly 'using'-declarations, instead of perhaps
with the opposite default behavior having to write 'hide'-declarations,
or simply acknowledging that the full effective interface of a derived
class really does include stuff from base classes.

Not much we can do about now, though.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 23 '06 #3
"bo*@blah.com" writes:
I know there exists a good reason why the designers of c++ decided that
function hiding should exist. But I don't know why. Can anybody provide
a good reason/example of a case where function hiding saves the day. I
know there exists one, I'd just like to hear about it.


There are at least two kinds of function hiding in C++ and I don't know
which one you are referring to. One kind is the static function definition
inherited from C. The other is making a member function private. In the
first case it would take a good reason to change the rules, it's just
confusing to people without any real gain. Recall that C did not have
namespaces and this was *a* cut at that problem. I could have a sine
function that worked in radians and another one that worked in degrees in
the same composite program, each being called with: sine(double).

In the case of member functions, it may be that the class designer does not
want everyone that wanders by to have access to all the functions. They may
do harm. And even if they do no harm, why clutter their (the user of the
class) mind with things they do not need to understand or even know about?
If a Fourier analysis program has a complicated function with ten
parameters, do I really need to know about this? I either trust the person
who provided the class or I do not. It is quite unlikely that I can fix his
goofs, if there are such.
Feb 23 '06 #4
Alf P. Steinbach ha scritto:
I find this reason less than convincing because (1) the hiding does not
help when the derived class offers no member function of that name, (2)
the hiding does not help with the "fragile base class" problem in
general, merely a subtle aspect that is not much of a practical problem,
and (3) the hiding is very counter-intuitive to most programmers (an
extreme case is where an automatically generated assignment operator in
a derived class hides a custom assignment operator in a base class), and
forces us to write extra, silly 'using'-declarations, instead of perhaps
with the opposite default behavior having to write 'hide'-declarations,
or simply acknowledging that the full effective interface of a derived
class really does include stuff from base classes.

What are 'using'-declarations?

Thanks,
Marco
Feb 23 '06 #5
* Marco Ambu:

What are 'using'-declarations?


struct Base
{
void foo( int ) {}
};

struct Derived: Base
{
using Base::foo;
void foo( char const [] ) {}
};

int main()
{
Derived d;
d.foo( 1234 ); // Should work, cause of 'using'.
}

On the other hand, 'hide'-declarations where just something I made up
for the discussion.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 23 '06 #6
hi,

I know this is going slightly off-topic from the OP question, but there
seems to be some thing that i dont understand so I thought this would
be the right place to ask about it,

I would really appreciate if you can explain a bit more on

struct Derived: Base
{
using Base::foo; // <--- What does this mean?
void foo( char const [] ) {}
};

I have used 'using' keyword many times, but i have never came across a
similar usage, or maybe you can refer me to to some good source where i
can refer to it :)

/hack_tick

Feb 24 '06 #7
Opps sorry there goes something from java, seems like i need to get
hold of my C++ again, i recalled what this is all about,

it is so that the base class functions are acessable in the deived
class.

Feb 24 '06 #8
hack_tick wrote:
hi,

I know this is going slightly off-topic from the OP question, but there
seems to be some thing that i dont understand so I thought this would
be the right place to ask about it,

I would really appreciate if you can explain a bit more on

struct Derived: Base
{
using Base::foo; // <--- What does this mean?
void foo( char const [] ) {}
};

I have used 'using' keyword many times, but i have never came across a
similar usage, or maybe you can refer me to to some good source where i
can refer to it :)

/hack_tick


Hi

In the example given above you would notice that the foo() method has
different signatures in the base and derived class. Thus its neither a
case of function over-riding (which needs same function signature in
base and derived class) nor of function over-loading (which needs same
function name with different signature in the SAME class). I

f we were not using the 'using' keyword, the base class foo() would
never be accessible using the derived's object. So the base class'
foo() would have got hidden since we are using a different signature of
the same function name in derived class.

Feb 24 '06 #9
I really feel this should be called function overloading since, base
class function is visible is derived class (after using it) and derived
class is having its own member function with same name but with
different signature, lemme know if I am wrong :)

/hack_tick

Feb 24 '06 #10

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

Similar topics

3
by: Cherrish Vaidiyan | last post by:
hello scholars, i am just learning PHP. i have a small requirement in urgency.i want to create a functionality of archiving in which a user can upload any file through a browser to a location....
1
by: Patrick Kowalzick | last post by:
Hello all, I want to test run-time if a template function for a special type exists. See example below. The checkfoo function is where I need some hints. Regards, Patrick #include...
9
by: Josiah Manson | last post by:
Hello. I am very new to Python, and have been unable to figure out how to check if a variable exists or not. In the following code I have made a kludge that works, but I think that it would be...
4
by: lyndon hughey | last post by:
I'm having a problem setting the testing for the existance of a nodes outerxml property. Below I try to test for the existance before I access the property, but I receive a null exception error. ...
4
by: Iain Porter | last post by:
Hi all, I have a usercontrol (a textbox with dropdown calendar that fills the textbox with the selected date) that I implement in a datagrid in a webform. On first loading the page, the datagrid's...
10
by: bienwell | last post by:
Hi, I have a question about file included in ASP.NET. I have a file that includes all the Sub functions (e.g FileFunct.vb). One of the functions in this file is : Sub TestFunct(ByVal...
27
by: sklett | last post by:
I just found myself doing something I haven't before: <code> public uint Duration { get { uint duration = 0; foreach(Thing t in m_things) { duration += t.Duration;
7
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
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
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
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.