Virtual function and overloading ?

V

Vajira

Hello,
Can you tell me why compilar does not recognize base class's virtual
function in the following code? Is there is any limitation in C++,
related to overloading virtual function of a base class in the derived
class ?

Compilar printed following message >
g++ main2.cpp
main2.cpp: In function `int main()':
main2.cpp:21: no matching function for call to `B::Display(int)'
main2.cpp:13: candidates are: virtual void B::Display()

Sample code --

#include <iostream>
using namespace std;

class A
{
public:
virtual void Display(int a) { cout << "Class A param " << a <<
endl;};
};

class B : public A
{
public:
virtual void Display() { cout << "Class B "<< endl;};

B(){;};
};

int main(void)
{
B* b = new B;
b->Display(10);

return 0;
}
 
W

wongjoekmeu

Hello Vajira,
Declaring virtual function in the base class is meant to be override in
the derived class. This is to use the advantage of being able to
declare an instnce of B to be of type A. This is correct since B is
derived from A.

By overriding it in the derive class when you say
A* myb = new B;
A->Display() it will automatically call the right display function
which is member of actually of B even though you declare it to be of
type A.
If you still want to use the Display function in class A which is being
hidden by the one in the derive class B. You have to fully qualify it
like
b->A::Display(10);

Robert
 
V

Vajira

Hello,
Thank for your replys. I have gone though the sites. It clearly
describes how C++ compilar handles this problem.

But still I coun't find why C++ compilar hinding overloaded member
function of base class.
1. Is there any advantage by hinding these functions ?
2. What is the logical reason for this ?

Sorry for bothering you guys, but I still curious of this matter.
Thank you......
 
H

Howard

Vajira said:
Hello,
Thank for your replys. I have gone though the sites. It clearly
describes how C++ compilar handles this problem.

But still I coun't find why C++ compilar hinding overloaded member
function of base class.
1. Is there any advantage by hinding these functions ?
2. What is the logical reason for this ?

Sorry for bothering you guys, but I still curious of this matter.
Thank you......

You wrote a virtual function in the base class. If you want your derived
class to modify what that function does, then you need to override it in the
derived class. And to properly override the function, it has to have the
same parameter list as the base class version. If it does not, then the
function in the derived class will "hide" the function of the same name in
the base class. That's just the rules.

You might ask yourself why you have two functions with the same name in your
base and derived classes, but with different parameter lists. The fact that
you made the base class function virtual implies that you wanted to override
it (or at least allow it to be overridden). But then you wrote a derived
class function that does *not* override it, because it's truly a different
function (as shown by its different parameter list).

So, what exactly do you want to do: override the function, or call a
different function? If the answer is "override it", then use identical
paremeter lists. And if the answer is "call a different function", then see
that previously stated FAQ for ways to accomplish that, or else just rename
the function in your derived class.

-Howard

(P.S., as to "why" something was done, that's not usually discussed here,
and isn't really important to getting the job done, right?)
 

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
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top