Why virtual function can not be overloaded?

P

PengYu.UT

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);
}
 
J

Jonathan Turkanis

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);
 
A

Alf P. Steinbach

* (e-mail address removed):
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>
 
M

Mathias Waack

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top