Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created July 13, 2019 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/55c226bd711e5ba55ee23d6848460a4c to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/55c226bd711e5ba55ee23d6848460a4c to your computer and use it in GitHub Desktop.
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