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