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_01Func(){ | |
Dog* d = new Dog("Tank"); //Should not use | |
shared_ptr<Dog> p(d); // p.get_count() == 1 | |
//Here when p goes out of scope, d will be destroyed. | |
//Then p2 goes out of scope, p2 will be destroyed again... | |
shared_ptr<Dog> p2(d); // p2.get_count() == 1, the counter won't increase | |
/* Lesson: An object should be assigned to a shrared_pointer | |
immediately when it is created. The above case does not follow | |
this rule. We first create the raw pointer d and then initialize | |
p and p2 with d. We should do somthing like: | |
shared_ptr<Dog> p(new Dog("Tank")), then shared_ptr<Dog> p2 = p.*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment