Skip to content

Instantly share code, notes, and snippets.

@plusangel
Created November 14, 2019 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plusangel/be5dfabaf756e82200823ecab91f8c3f to your computer and use it in GitHub Desktop.
Save plusangel/be5dfabaf756e82200823ecab91f8c3f to your computer and use it in GitHub Desktop.
vtbl example
#include <iostream>
class Base {
public:
virtual void function1() { std::cout << "Base::function1()" << std::endl; }
virtual void function2() { std::cout << "Base::function2()" << std::endl; }
virtual ~Base() { std::cout << "Base destructor" << std::endl; }
};
class Derived1 : public Base {
virtual void function1() override { std::cout << "Base::function1()" << std::endl; }
virtual ~Derived1() { std::cout << "Derived1 destructor" << std::endl; }
};
class Derived2 : public Base {
virtual void function2() override { std::cout << "Base::function2()" << std::endl; }
virtual ~Derived2() { std::cout << "Derived2 destructor" << std::endl; }
};
int main() {
Derived1* d1 = new Derived1;
Base* b = d1;
b->function1();
b->function2();
delete(b);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment