dragosb
dragosb

Reputation: 627

Virtual function overloading

Lets say that we have :

Class A
{
public:
   virtual void print(){ std::cout<<" A "<<endl; }
}

Class B : public A
{
public:
   virtual void print(int x){ std::cout<<" B "<<endl;}

}

I thought that the definition in Class B of function print will hide the function print from class A. But the following code works and prints " A "

int main()
{

A * a = new B;

a->print();

return 0;
}  

If I write the main function like this it doesnt work :

int main()
{

B b;

b.print();

return 0;
}  

What i Want to know is...in my first main() example I have a B object that calls print()...shouldnt then print() be hidden and have an error like in the second main() example

Upvotes: 3

Views: 1813

Answers (5)

Pete Becker
Pete Becker

Reputation: 76245

The issue here is name lookup. In a call like A *a = new B; a->print() the compiler looks at the type of a, which is A*, and looks in the class A for a member function named print. It finds it and it calls it. Similarly, with B *b = new B; b->print(); the compiler looks in class B for a member function named print; it finds print(int), which can't be called with no arguments. And because it found a function named print in B it stops looking; it doesn't go into A to find A::print(). That's name hiding.

The key here is that name lookup starts with the declared type of the object; in these two examples the types are A* and B*, respectively. Lookup does not pay attention to the actual type of the thing that a pointer or reference points to or refers to.

Upvotes: 2

Ivan Smirnov
Ivan Smirnov

Reputation: 4435

That is because two print() functions have different signatures. Two functions - print() and print(int) are considered different and cannot be overloaded overridden by each other.

Upvotes: 1

blackbird
blackbird

Reputation: 967

The member function print() in B is not overriding the print() in A because it has a different signature. Therefore, when calling print() in your first, unedited version of main(), there is only one matching function to call, as pointed out by user juanchopanza: A::print().

EDIT: To summarize:

  • Polymorphic behavior: If A::print() and B::print() had the same signature, the appropriate print() would be chosen at runtime as long as you reference the object through pointers or references.

  • Function overloading: Since a class is a scope and functions do not overload across scopes, the functions from base classes are hidden by functions of the same name in a derived class. For this, the type of the variable you are using to refer to your object is the one that matters, not the type of the object itself. Therefore, in your second example you get an error that there is no matching function to call, but in your first example, only one function is in scope.

Upvotes: 7

Anthony
Anthony

Reputation: 12387

Check your class definitions; the print functions in A and B have different signatures. When you call

a->print();

There's only one function that fits that signature, which is A::print(). If you remove the int parameter from B's definition you should observe the behaviour you're expecting.

Upvotes: 0

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153802

Named overloading functions in a derived class are hiding those from the base class. However, you'll need to use the derived class interface to see so:

B b;
b.print(); // won't work

Upvotes: 1

Related Questions