Skip to content

Instantly share code, notes, and snippets.

@csullivan
Created May 11, 2016 20:23
Show Gist options
  • Save csullivan/d0ff81666fcfdf56390178019bfd9c59 to your computer and use it in GitHub Desktop.
Save csullivan/d0ff81666fcfdf56390178019bfd9c59 to your computer and use it in GitHub Desktop.
Illustrating the difference between virtual function overrides and non-virtual derived class methods with the same name and function signature
#include <iostream>
using namespace std;
class Base {
public:
Base(){;}
virtual ~Base(){;}
void Func() {
cout << "Base::Func" << endl;
return;
}
virtual void vFunc() {
cout << "Base::vFunc" << endl;
return;
}
};
class Derived : public Base {
public:
Derived(){;}
~Derived() override {;}
void Func() {
cout << "Derived::Func" <<endl;
return;
}
virtual void vFunc() override {
cout << "Derived::vFunc" <<endl;
return;
}
};
int main() {
Derived* d = new Derived;
d->Func();
d->vFunc();
cout << endl;
Base* b = static_cast<Derived*>(d);
b->Func();
b->vFunc();
b->Base::vFunc();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment