shared_ptr的 reset用法
#include <iostream> #include <future> #include <thread> using namespace std; class Person { public: Person(int v) { value = v; std::cout << "Cons" <<value<< std::endl; } ~Person() { std::cout << "Des" <<value<< std::endl; } int value; }; int main() { std::shared_ptr<Person> p1(new Person(1));// Person(1)的引用计数为1 std::shared_ptr<Person> p2 = std::make_shared<Person>(2); p1.reset(new Person(3));// 首先生成新对象,然后引用计数减1,引用计数为0,故析构Person(1) // 最后将新对象的指针交给智能指针 std::shared_ptr<Person> p3 = p1;//现在p1和p3同时指向Person(3),Person(3)的引用计数为2 p1.reset();//Person(3)的引用计数为1 p3.reset();//Person(3)的引用计数为0,析构Person(3) return 0; }
root@ubuntu:~/c++# ./reset
Cons1
Cons2
Cons3
Des1
Des3
Des2
注意,不能将一个原始指针直接赋值给一个智能指针,如下所示,原因是一个是类,一个是指针。
std::shared_ptr<int> p4 = new int(1);// error
reset()包含两个操作。当智能指针中有值的时候,调用reset()会使引用计数减1.当调用reset(new xxx())重新赋值时,智能指针首先是生成新对象,然后将就对象的引用计数减1(当然,如果发现引用计数为0时,则析构旧对象),然后将新对象的指针交给智能指针保管。
- 获取原始指针
std::shared_ptr<int> p4(new int(5)); int *pInt = p4.get();
#include <iostream> #include <future> #include <thread> using namespace std; class Person { public: Person(int v) { value = v; std::cout << "Cons" <<value<< std::endl; } ~Person() { std::cout << "Des" <<value<< std::endl; } int value; }; int main() { std::shared_ptr<Person> p1(new Person(1));// Person(1)的引用计数为1 std::shared_ptr<Person> p2 = std::make_shared<Person>(2); p1.reset(new Person(3));// 首先生成新对象,然后引用计数减1,引用计数为0,故析构Person(1) // 最后将新对象的指针交给智能指针 std::shared_ptr<Person> p3 = p1;//现在p1和p3同时指向Person(3),Person(3)的引用计数为2 p1.reset();//Person(3)的引用计数为1 p3.reset();//Person(3)的引用计数为0,析构Person(3) p3.reset();//再reset return 0; }
root@ubuntu:~/c++# g++ -std=c++11 reset2.cpp -o reset2
root@ubuntu:~/c++# ./reset2
Cons1
Cons2
Cons3
Des1
Des3
Des2
删除器 lambda
#include <iostream> #include <memory> using namespace std; class obj { private: int _num; public: obj(int num):_num(num) { cout << "obj is constructed and num : " << num << endl; } ~obj() { cout << "obj is deleted and num " << _num << endl; } }; void share_ptr() { std::shared_ptr<obj> shared_ptr1(new obj(99)); obj * p = shared_ptr1.get(); cout << " addr " << (long)p << endl; shared_ptr1.reset(new obj(33), [](obj* p1){ cout<<"in reset" << " addr " << (long)p1 <<endl; delete p1; }); //p = shared_ptr1.get(); //cout << " addr " <<hex << (long)p <endl; } int main() { share_ptr(); return 0; }
oot@ubuntu:~/c++# g++ -std=c++11 share2.cpp -o share2 root@ubuntu:~/c++# ./share2 obj is constructed and num : 99 addr 187651489353328 obj is constructed and num : 33 obj is deleted and num 99 in reset addr 187651489354432
---------------------------- 先delete 99 后reset
obj is deleted and num 33
#include <iostream> #include <memory> using namespace std; class obj { private: int _num; public: obj(int num):_num(num) { cout << "obj is constructed and num : " << num << endl; } ~obj() { cout << "obj is deleted and num " << _num << endl; } }; void share_ptr() { std::shared_ptr<obj> shared_ptr1(new obj(99)); obj * p = shared_ptr1.get(); cout << " addr " << (long)p << endl; shared_ptr1.reset(new obj(33), [](obj* p1){ cout<<"in reset" << " addr " << (long)p1 <<endl; }); p = shared_ptr1.get(); cout << " addr " << (long)p << endl; } int main() { share_ptr(); return 0; }
root@ubuntu:~/c++# g++ -std=c++11 share2.cpp -o share2 root@ubuntu:~/c++# ./share2 obj is constructed and num : 99 addr 187651132960368 obj is constructed and num : 33 obj is deleted and num 99 ---自动调用析构 addr 187651132961472 in reset addr 187651132961472 root@ubuntu:~/c++#