By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Virtual function and overloading ?

P: n/a
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;
}

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Vajira wrote:
Hello, Hi,
Can you tell me why compilar does not recognize base class's virtual
function in the following code?

Because A::Display(int) is hidden by B::Display().
See:
http://www.parashift.com/c++-faq-lit....html#faq-23.6
..
..
Bogdan

Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a
"Vajira" <va*****@gmail.com> wrote in message news:<11**********************@z14g2000cwz.googleg roups.com>...
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;
}


Check this out for an answer
http://www.parashift.com/c++-faq-lit....html#faq-23.6
Jul 23 '05 #4

P: n/a
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......

Jul 23 '05 #5

P: n/a

"Vajira" <va*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?)
Jul 23 '05 #6

P: n/a
Thanks for all your replies.

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.