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 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