Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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