Created
September 29, 2014 08:54
-
-
Save zhangyuchi/d7e62bda89804ed8238f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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