472,353 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Virtual function and overloading ?

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
6 7497
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
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
"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
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

"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
Thanks for all your replies.

Jul 23 '05 #7

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

Similar topics

6
by: Victor | last post by:
Anyone knows how to write a virtual function for operator<< ? I have a base class and some public derived class from it. For the derived class, I...
1
by: A. Saksena | last post by:
Hi all, I am trying to compile following code :- ======================================= #include <iostream.h> #include <string> namespace tlm...
11
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class,...
6
by: Ryan H. | last post by:
A very simple example of something that acts quite odd: class TopClass { public: TopClass(){ } virtual ~TopClass(){ } virtual int myFunc() =...
13
by: George | last post by:
Hi all, I'm moving my experince from C to C++ and while so I'm facing some problems with inheritance. Here is my problem: class A { // All...
7
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.