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

Inheritance and sharing function names

Hi,

Here's a very simple inheritance case that cause me some problems:

class A
{
public:
void Bob(int i) { cout << "in A::Bob(int)" << endl; }
};

class B : public A
{
public:
void Bob() { cout << "in B::Bob" << endl; }
};

main()
{
A a;
B b;

a.Bob(0);
b.Bob();
b.Bob(0); << ERROR: No Bob(int) can be found
}

On the last line, my compilers (GCC 3.3.5 and 3.4.2) tells me that no
Bob(int) can be found. Without the Bob() method in class B, there's no
problem.

This is incredible for me, I probably miss something somewhere, cause
this should be very basic C++ stuff. The compiler should check if a
method is defined in a class, and if not, it checks in the base class.
So why it doesn't do the same with function sharing the same name? Any
Help?

Thanks,
Karl

Jul 23 '05 #1
4 1314
kl******@sunrisetelecom.com wrote:
Here's a very simple inheritance case that cause me some problems:
[...classical example of name hiding snipped...]
On the last line, my compilers (GCC 3.3.5 and 3.4.2) tells me that no
Bob(int) can be found. Without the Bob() method in class B, there's no
problem.

This is incredible for me, I probably miss something somewhere, [...]


Look up "name hiding" in the FAQ (http://www.parashift.com/c++-faq-lite/).
Jul 23 '05 #2
<kl******@sunrisetelecom.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

Here's a very simple inheritance case that cause me some problems:

#include <iostream>
using std::cout;

#include <ostream>
using std::endl;
class A
{
public:
void Bob(int i) { cout << "in A::Bob(int)" << endl; }
};

class B : public A
{
public:
void Bob() { cout << "in B::Bob" << endl; }
};

main()
int main()
{
A a;
B b;

a.Bob(0);
b.Bob();
b.Bob(0); << ERROR: No Bob(int) can be found
}

On the last line, my compilers (GCC 3.3.5 and 3.4.2) tells me that no
Bob(int) can be found.
That's as it should be.
Without the Bob() method in class B, there's no
problem.
That's as it should be.

This is incredible for me, I probably miss something somewhere,
Yes. :-)
cause this should be very basic C++ stuff.
It is. Research 'name lookup', and 'name hiding'.
The compiler should check if a
method is defined in a class,
Yes. And there is no function called 'Bob' with an argument
of type 'int' visible in class 'B'. (The argument list is
also considered when the compiler looks up a function).
and if not, it checks in the base class.
But there's no 'if not' in this case.
So why it doesn't do the same with function sharing the same name?


The name 'B::bob()' hides the name 'B::bob(int)'
See above. Which C++ book(s) are you reading which does
not explain this?

-Mike
Jul 23 '05 #3
It will work if you do using A::Bob in B:s declaration.

class B : public A
{
public:
using A::Bob;
void Bob() { cout << "in B::Bob" << endl; }
};

Jul 23 '05 #4

ja*******@gmail.com wrote:
It will work if you do using A::Bob in B:s declaration.

class B : public A
{
public:
using A::Bob;
void Bob() { cout << "in B::Bob" << endl; }
};


U are expecting the function Bob to get overloaded. But for overloading
to tkae place the two unctions should be in the same scope. Here the
scope are different. Hence the error.

Jul 23 '05 #5

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

Similar topics

5
by: Robert Spoons | last post by:
Can you look over this code, preferably try it, and comment? I believe the 'extend' function below will allow you to use full 'class inheritance' in javascript, but I would like to verify it. ...
7
by: Morten Aune Lyrstad | last post by:
Hi there! I have written this message before, but for some reason it did not appear to show up on the board. If it does, then I have a problem with my newsgroup app, and I apologize for double...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
31
by: John W. Kennedy | last post by:
I quite understand about prototypes and not having classes as such, but I happen to have a problem involving blatant is-a relationships, such that inheritance is the bloody obvious way to go. I can...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
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....
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
1
by: Wilson | last post by:
hi. Again trying to understand inheritcance but can't see what is wrong with this small peice of code below to test my knowledge. When complied it simply returns the "system("PAUSE")" and nothing...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.