1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| int main() { shared_ptr<Person> person1; shared_ptr<Person> person2(nullptr); shared_ptr<Person> person3(new Person(10));
shared_ptr<Person> person4 = std::make_shared<Person>(5); //效率更高,内存分布在一起 shared_ptr<Person> person5(std::move(person3)); // person3无法再使用 shared_ptr<Person> arary(new Person[10], deletePersonArray); weak_ptr<Person> weak_Person = person5; cout << weak_Person.use_count() << endl; shared_ptr<Person> person6 = person5; cout << weak_Person.use_count() << endl; person5.reset(); cout << weak_Person.use_count() << endl; person6.reset(); if (weak_Person.expired()) { cout << weak_Person.use_count() << endl; auto shareptr = weak_Person.lock(); cout << shareptr << endl; } return 0; }
|