Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
void foo(unique_ptr<Dog> p){
p->bark();
}
unique_ptr<Dog> getDog(){
unique_ptr<Dog> p(new Dog("Jack"));
//return p will use the move semantics!
//so p will no longer has the ownership of Jack
return p;
}
void test03(){
unique_ptr<Dog> pD (new Dog("DND"));
//Cannot directly pass to foo(), since pD is unique pointer.
//pD will be destroyed in foo().
foo(move(pD));
//pD2 owns the jack now, so it's not nullptr!
unique_ptr<Dog> pD2 = getDog();
if(!pD2) {cout << "pD2 is nullptr. " << endl;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment