深入学习c++--智能指针(一) shared_ptr
2019-04-30 00:17
295 查看
目录
1. 几种智能指针
[code]1. auto_ptr: c++11中推荐不使用他 2. shared_ptr: 每添加一次引用 就+1,减少一次引用,就-1;做到指针进行共享 3. unique_ptr: 一个指针同时只能有一个使用者使用 4. weaked_ptr: 与shared_ptr搭配使用
1.1 shared_ptr
参考:https://zh.cppreference.com/w/cpp/memory/shared_ptr
std::shared_ptr是通过指针保持对象共享所有权的智能指针。多个
shared_ptr对象可占有同一对象。下列情况之一出现时销毁对象并解分配其内存:
[code]#include <iostream> #include <memory> #include <thread> #include <chrono> #include <mutex> class Object { public: Object(int id) : m_id(id) { std::cout << "init obj " << m_id << std::endl; } ~Object() { std::cout << "bye bye " << m_id << std::endl; } int id() const { return m_id; } private: int m_id; }; typedef std::shared_ptr<Object> ObjectPtr; void print(ObjectPtr obj); void printRef(const ObjectPtr& obj); void interfaceOfSharedPtr() { ObjectPtr null; std::cout << "ref count is " << null.use_count() << std::endl; // 0 ObjectPtr obj(new Object(1)); std::cout << "ref count is " << obj.use_count() << std::endl; // 1 ObjectPtr obj2(obj); std::cout << "ref count is " << obj.use_count() << std::endl; // 2 ObjectPtr obj3 = obj; std::cout << "ref count is " << obj.use_count() << std::endl; // 3 obj2.reset(); // 不需要了 //obj2 = nullptr; // c++11写法 std::cout << "ref count is " << obj.use_count() << std::endl; // 2 ObjectPtr obj4; obj3.swap(obj4); //std::swap(obj3, obj4); // 本身管理资源,引用数没有变化 std::cout << "ref count is " << obj.use_count() << std::endl; // 2 auto p = obj.get(); if (p) { std::cout << "id is " << p->id() << std::endl; } if (obj) { std::cout << "p id is " << obj->id() << std::endl; // operator -> std::cout << "ref id is " << (*obj).id() << std::endl; // operator * } obj4 = nullptr; // if (obj.unique()) //效率更高 // if (obj.use_count() == 1) // 会有效率损失 std::cout << "only one hold ptr " << obj.unique() << std::endl; // 值传入 print(obj); std::cout << "ref count is " << obj.use_count() << std::endl; // 引用传入, 推荐 printRef(obj); } void print(ObjectPtr obj) // 值传入,会临时改变引用数量+1 { std::cout << "count " << obj.use_count() << " id " << obj->id() << std::endl; // 3 } void printRef(const ObjectPtr& obj) // 引用传递不会改变引用数量 { std::cout << "ref count " << obj.use_count() << " id " << obj->id() << std::endl; // 2 } void deleteOfObject(Object* obj) { if (obj) { std::cout << "delete obj " << obj->id() << std::endl; delete obj; } } void useDeleter() { // 管理 裸指针 和 处理这个指针的函数 -- 可以管理应用delete资源,也可以管理其他类似资源 ObjectPtr obj(new Object(2), deleteOfObject); ObjectPtr obj2 = obj; } int main() { interfaceOfSharedPtr(); return 0; }
[code]ref count is 0 init obj 1 ref count is 1 ref count is 2 ref count is 3 ref count is 2 ref count is 2 id is 1 p id is 1 ref id is 1 only one hold ptr 1 count 2 id 1 ref count is 1 ref count 1 id 1 bye bye 1
相关文章推荐
- 深入学习c++--智能指针(二) weak_ptr (打破shared_ptr循环引用)
- C++学习之智能指针--auto_ptr、scoped_ptr、scoped_array、shared_ptr、shared_array、weak_ptr
- C/C++ 日常学习总结(第二十一篇)智能指针shared_ptr
- C++智能指针shared_ptr剖析
- C++常用的智能指针auto_ptr、shared_ptr
- C++智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> )
- [置顶] 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- C++智能指针(三):weak_ptr--解决shared_ptr循环引用问题
- c++中关于智能指针std::tr1::shared_ptr的用法
- c++智能指针《二》 std::tr1::shared_ptr
- 深入学习c++--智能指针(四)--使用建议
- 详解C++各种智能指针: auto_ptr, shared_ptr, weak_ptr, scoped_ptr
- c++智能指针:auto_ptr shared_ptr
- C++中的智能指针auto_ptr,shared_ptr
- C++智能指针:auto_ptr、shared_ptr、weak_ptr等
- C++ 智能指针 shared_ptr
- 深入理解C++中的智能指针auto_ptr
- 我所熟悉的C++智能指针auto_ptr vs shared_ptr (一)
- 【C++】智能指针的作用,模拟实现auto_ptr,scoped_ptr,shared_ptr,scoped_array,shared_array
- 【C++标准模板库笔记2】智能指针shared_ptr的简单使用