Created
November 14, 2019 18:55
-
-
Save plusangel/be5dfabaf756e82200823ecab91f8c3f to your computer and use it in GitHub Desktop.
vtbl example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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