Last active
October 7, 2018 21:10
-
-
Save odeblic/fa54037bf4d764a5dc02735cb4bd79f3 to your computer and use it in GitHub Desktop.
Smart destructor
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> | |
#include <memory> | |
struct Base | |
{ | |
~Base() | |
{ | |
std::cout << "~Base()" << std::endl; | |
} | |
}; | |
struct Derived : Base | |
{ | |
~Derived() | |
{ | |
std::cout << "~Derived()" << std::endl; | |
} | |
}; | |
std::shared_ptr<Base> Factory(bool flag) | |
{ | |
if (flag) | |
{ | |
return std::make_shared<Base>(); | |
} | |
else | |
{ | |
return std::make_shared<Derived>(); | |
} | |
} | |
int main() | |
{ | |
std::shared_ptr<Base> sp1 = Factory(false); | |
std::shared_ptr<Base> sp2 = Factory(true); | |
return 0; | |
} | |
// Source of inspiration: | |
// https://www.youtube.com/watch?v=ZiNGWHg5Z-o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment