Skip to content

Instantly share code, notes, and snippets.

@zhangyuchi
Created September 29, 2014 08:54
Show Gist options
  • Save zhangyuchi/d7e62bda89804ed8238f to your computer and use it in GitHub Desktop.
Save zhangyuchi/d7e62bda89804ed8238f to your computer and use it in GitHub Desktop.
#include <iostream>
#include <memory>
struct Foo {
Foo() { std::cout << "Foo...\n"; }
~Foo() { std::cout << "~Foo...\n"; }
};
struct D {
void operator() (Foo* p) {
std::cout << "Calling delete for Foo object... \n";
delete p;
}
};
int main()
{
std::cout << "Creating new Foo...\n";
std::unique_ptr<Foo, D> up(new Foo(), D()); // up owns the Foo pointer (deleter D)
std::cout << "Replace owned Foo with a new Foo...\n";
up.reset(new Foo()); // calls deleter for the old one
std::cout << "Release and delete the owned Foo...\n";
up.reset(nullptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment