473,464 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Inheritance, Overloading, and Candidate Lists


I can work around this easily enough, but can someone help me
understand why this works as it does? It seems that functions from a
grandparent class are not available as candidates for overload
resolution in the grandchild class?

/***** Begin Example *****/
#include <iostream>

class Foo
{
public:
void somefunc(int a);
};

class Bar: public Foo
{
public:
void somefunc(int a, int b);
};

class Baz: public Bar
{
public:
void broken();
};

void Foo::somefunc(int a)
{
std::cout << "somefunc 1 arg: " << a << std::endl;
return;
}

void Bar::somefunc(int a, int b)
{
std::cout << "somefunc 2 args: " << a << ", " << b << std::endl;
return;
}

void Baz::broken()
{
int arg = 555;
// Foo::somefunc(arg); <-- This works
somefunc(arg); // <-- Why doesn't this work?
return;
}

int main(int argc, char** argv)
{
class Baz tmp;
tmp.broken();
return(0);
}

/***** Begin Example *****/

Oct 12 '07 #1
3 1604
Dan Noland <no******@gmail.comwrote in news:1192212441.350855.171600
@y27g2000pre.googlegroups.com:
>
I can work around this easily enough, but can someone help me
understand why this works as it does? It seems that functions from a
grandparent class are not available as candidates for overload
resolution in the grandchild class?

/***** Begin Example *****/
#include <iostream>

class Foo
{
public:
void somefunc(int a);
};

class Bar: public Foo
{
public:
void somefunc(int a, int b);
};

class Baz: public Bar
{
public:
void broken();
};
See the C++ FAQ, section 23.9

http://www.parashift.com/c++-faq-lit....html#faq-23.9

Oct 12 '07 #2
On Oct 12, 4:06 pm, Andre Kostur <nntps...@kostur.netwrote:
Dan Noland <nolan...@gmail.comwrote in news:1192212441.350855.171600
@y27g2000pre.googlegroups.com:


I can work around this easily enough, but can someone help me
understand why this works as it does? It seems that functions from a
grandparent class are not available as candidates for overload
resolution in the grandchild class?
/***** Begin Example *****/
#include <iostream>
class Foo
{
public:
void somefunc(int a);
};
class Bar: public Foo
{
public:
void somefunc(int a, int b);
};
class Baz: public Bar
{
public:
void broken();
};

See the C++ FAQ, section 23.9

http://www.parashift.com/c++-faq-lit....html#faq-23.9
Thanks, I just found a similar explanation in section 19.3.1 of
Lippman and Lajoie. It seems strange that the shadowing is done by
function name rather than function prototype, but this works. Do any
language/compiler gurus know why this is the case?

YT,
Dan Noland
http://nolandda.org/

Oct 12 '07 #3
On 12 Oct, 22:18, Dan Noland <nolan...@gmail.comwrote:
>
Thanks, I just found a similar explanation in section 19.3.1 of
Lippman and Lajoie. It seems strange that the shadowing is done by
function name rather than function prototype, but this works. Do any
language/compiler gurus know why this is the case?

YT,
Dan Nolandhttp://nolandda.org/

It's a safety measure against very difficult bugs.
Suppose you are using your preferred library in your project. This
library has a very deep and complex hierarchy, you create your class
subclassing from a class of this library:

class myclass : dialog_box
{

.... cut ...

void clear(long id);

};

Now suppose that somewhere in the library there is a class widget and
dialog_box is derived from that.
class widget
{

.... cut ...

void clear(int id);

};
and *you are not aware of the exsistance of widget::clear()* it can
happen in very big libraries, as example GUI libraries.
Now somewhere in your code you write:

myclass c;
c.clear(7);

Q: what member do you think compiler will call?

A: a conforming compiler will call _fortunatly_ your myclass::clear()
function, but if the hidden rule was not present the compiler would
have called widget::clear() instead.

If you don't know about widget::clear() finding the bug could span
from difficult to nightmare!

Proably who introduced such a rule in C++ was bitten by something like
this ;-)

Oct 13 '07 #4

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

Similar topics

1
by: Fuzzyman | last post by:
I've been programming in python for a few months now - and returning to programming after a gap of about ten years I've really enjoyed learning python. I've just made my first forays into...
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
0
by: JKJ | last post by:
I'm not an OOP guru, but I would say ". . .not necessarily" What if the base class and derived classes are internal? Then the base class has to be within the same assembly to be inherited from. ...
6
by: apm | last post by:
Recently I have had to use a value type for a complex structure because I don't know how to override the = operator. Can the operator ever be overloaded? Or can inheritance be used with value types?
6
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
11
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.