473,387 Members | 1,431 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,387 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 7109
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.