Inheritance and sharing function names

K

klessard

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
 
V

Victor Bazarov

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/).
 
M

Mike Wahler

Hi,

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

#include <iostream>
using std::cout;

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
 
J

jacobsson

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; }
};
 
A

Archie

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.
 

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

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,651
Latest member
VeraPiw932

Latest Threads

Top