Let's assume this scenario in Visual C++ 2010:
#include <iostream>
using namespace std;
struct Base {
void Display() {
cout << "Base: Non-virtual display." << endl;
};
virtual void vDisplay() {
cout << "Base: Virtual display." << endl;
};
};
struct Derived : Base {
void Display() {
cout << "Derived: Non-virtual display." << endl;
};
virtual void vDisplay() {
cout << "Derived: Virtual display." << endl;
};
};
int main() {
Base ba;
Derived de;
ba.Display();
ba.vDisplay();
de.Display();
de.vDisplay();
};
Theoretically, the output of this little application should be:
Base: Non-virtual display.
Base: Virtual display.
Base: Non-virtual display.
Derived: Virtual display.
because the Display
method of the Base
class is not a virtual
method so the Derived
class should not be able to override it. Right?
The problem is that when I run the application, it prints this:
Base: Non-virtual display.
Base: Virtual display.
Derived: Non-virtual display.
Derived: Virtual display.
So either I don't understand the concept of virtual
methods or something strange happens in Visual C++.
What is the explanation?
de.Base::Display()
. – Cavie