Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created July 13, 2019 17:16
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/b57a2ae4e6d79083163cc5c7cdc6f8da to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/b57a2ae4e6d79083163cc5c7cdc6f8da to your computer and use it in GitHub Desktop.
void test02(){
//unique pointer can access the same object at different time
unique_ptr<Dog> pD (new Dog("Gunner"));
unique_ptr<Dog> pD1 (new Dog("Smile"));
pD1->bark();
//Smile will be destroyed, and pD1 owns pD now
pD1 = move(pD);
pD1->bark();
//release() function will return the raw pointer. It will also
//change the ownership of unique pointer and set it to nullptr.
Dog* p = pD1.release();
//reset pD to other object, if unique pointer originally owns an
//object, that object will be deleted!
//pD.reset(new Dog(smokey));
//same as pD = nullptr
//pD.reset();
if(!pD1) { // pD1 is empty now
cout << "pD1 is empty now." <<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment