473,804 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7589
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.go oglegroups.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.goo glegroups.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
7224
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 hope they can use << to output their different data. What's more, I want the base class pointers can access the derived ones and then their corresponding operator <<. For example: class base { // virtual and friend can't be put together, so the following // code won't work.
1
2344
by: A. Saksena | last post by:
Hi all, I am trying to compile following code :- ======================================= #include <iostream.h> #include <string> namespace tlm { template <typename REQUEST, typename RESPONSE > class tlm_transport_if {
11
8927
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
39
2192
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 wasn't it introduced to the ANSI? 3. Is there any C implementation supporting this feature? I assume some of you will claim that there is no need in function overloading, so I would like to know your arguments too. Thanks,
45
3296
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
2
2519
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, except for the return type which may return a pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter of the function can be a derived reference from the base class's reference also. This dichotomy...
6
2275
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() = 0; };
13
3664
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 share attributes // ....
7
6132
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.