Created
July 13, 2019 17:16
-
-
Save zhangxiaomu01/b57a2ae4e6d79083163cc5c7cdc6f8da to your computer and use it in GitHub Desktop.
This file contains 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
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