Skip to content

Instantly share code, notes, and snippets.

@odeblic
Last active October 7, 2018 21:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save odeblic/fa54037bf4d764a5dc02735cb4bd79f3 to your computer and use it in GitHub Desktop.
Save odeblic/fa54037bf4d764a5dc02735cb4bd79f3 to your computer and use it in GitHub Desktop.
Smart destructor
#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