472,988 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 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 4908
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: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.