473,395 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Why virtual function can not be overloaded?

The following program shows that virtual function can not be
overloaded.

Could you tell me the reasons from the C++ compiler point of view?
Thanks!

Peng

#include <iostream>

class B{
public:
virtual void doit(){
std::cout << "in B" << std::endl;
}
void doit(int i){
std::cout << i << std::endl;
doit();
}
};

class D: public B{
public:
virtual void doit(){
std::cout << "in D" << std::endl;
}
};

int main(int argc, char *argv[]) {
D d;
d.doit(1);
}

Sep 23 '05 #1
3 7111
Pe*******@gmail.com wrote:
The following program shows that virtual function can not be
overloaded.

Could you tell me the reasons from the C++ compiler point of view?
Thanks!

Peng

#include <iostream>

class B{
public:
virtual void doit(){
std::cout << "in B" << std::endl;
}
void doit(int i){
std::cout << i << std::endl;
doit();
}
};

class D: public B{
public:
using B::doit;
virtual void doit(){
std::cout << "in D" << std::endl;
}
};

int main(int argc, char *argv[]) {
D d;
d.doit(1);
}


You can also invoke d.doit() as follows

B& b = d;
b.doit(1);

--
Jonathan Turkanis
www.kangaroologic.com

Sep 23 '05 #2
* Pe*******@gmail.com:
The following program shows that virtual function can not be
overloaded.

Could you tell me the reasons from the C++ compiler point of view?
Thanks!

Peng

#include <iostream>

class B{
public:
virtual void doit(){
std::cout << "in B" << std::endl;
}
void doit(int i){
std::cout << i << std::endl;
doit();
}
};

class D: public B{
public:
using B::doit;
virtual void doit(){
std::cout << "in D" << std::endl;
}
};

int main(int argc, char *argv[]) {
D d;
d.doit(1);
}


FAQ 23.7,
<url: http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.7>

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 23 '05 #3
Pe*******@gmail.com wrote:
The following program shows that virtual function can not be
overloaded.
There's a subtle difference between "can not be overloaded" and "I
don't understand what happens".
Could you tell me the reasons from the C++ compiler point of view?
The compiler follows the C++ standard, thats its job.
#include <iostream>

class B{
public:
virtual void doit(){
std::cout << "in B" << std::endl;
}
void doit(int i){
std::cout << i << std::endl;
doit();
}
};

class D: public B{
public:
virtual void doit(){
std::cout << "in D" << std::endl;
}
};

int main(int argc, char *argv[]) {
D d;
d.doit(1);
}


D::doit() hides all methods with the same name for all base classes.
You'd better write d.B::doit(1). You can easily import these methods
using a "using" declaration. Try this:

class D: public B{
public:
virtual void doit(){
std::cout << "in D" << std::endl;
}
using B::doit;
};

The "using" imports all methods with name "doit" from B's into A's
namespace.

Mathias
Sep 23 '05 #4

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

Similar topics

3
by: Philippe Guglielmetti | last post by:
Look at these few lines of code: class A { public: virtual void f() { cout << "A";}}; class B : public A{public: static void f() { cout << "B"; }}; class C : public B{public: void f() { cout <<...
7
by: Karsten Hochkirch | last post by:
Hi, I have just encountered a problem when calling a virtual member function in a constructor. Only the memberfunction of the parent class is called instead of the overloaded function. I...
12
by: placid | last post by:
Hi if a have the following classes class A { public: A(); virtual ~A(); virtual string someFunction() const = 0;
4
by: pocmatos | last post by:
Hi all, I have an abstract class acting as interface to a given class of objects. And mostly everywhere around my program I'm passing things like: vector<int>, list<unsigned long>,...
1
by: ravinderthakur | last post by:
hi all experts, i was just thinking about making overloaded operators virtual. i was wordering what could be the implications of such scenarios. i will be thankful if somebody can provide...
20
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got...
6
by: Bit byte | last post by:
I have a class BaseApplication, from which I am deriving two classes A and B. In class BaseApplication, I have a virtual method as ff: virtual void saveToDb(const char*); I will like to...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
7
by: Mark | last post by:
Hi, I have an abstract class which I want my other classes to inherit from. In the constructor of the abstract class I want to check if certain virtual functions have been overloaded or not. Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.