C++Boost库学习之smart_ptr库
2018-09-11 17:20
99 查看
目录
1.shared_ptr
使用例子
2.weak_ptr
使用例子
3.scoped_ptr
使用例子
4.shared_array与scoped_array
使用例子
1.shared_ptr ^
shared_ptr智能指针也跟其名字一样,共享的,允许多个该智能指针共享地“拥有”同一堆分配对象的内存。实现上采用了引用计数,只有在引用计数归零时,才会真正释放所占有的堆内存的空间。
使用例子 ^
#include <iostream> #include <boost/smart_ptr/shared_ptr.hpp> using namespace boost; using std::cout; using std::endl; struct A { int a = 10; }; int main() { A *p = new A; shared_ptr<A> ptr(new A(*p)); shared_ptr<A> ptr1(ptr); cout << "判断目标是否是原始数据:"<<ptr1.owner_before(shared_ptr<A>(new A(*p))) << endl; cout << "共享指针的数量:" << ptr.use_count() << endl; cout << "通过指针访问成员:" << ptr.get()->a << endl; cout << "是否是单个:" << ptr.unique() << endl; cout << "是否一样:" << ptr._internal_equiv(ptr1) << endl; cout << "当前内部共享指针的数量:" << ptr._internal_count().use_count() << endl; //使当前指针指向nullptr,引用计数减少1 ptr1.reset(); cout << "当前内部共享指针的数量:" << ptr._internal_count().use_count() << endl; ptr.reset(); cout << "当前内部共享指针的数量:" << ptr._internal_count().use_count() << endl; cout << "内部共享指针的数量是否为空:"<<ptr._internal_count().empty() << endl; shared_ptr<A> ptr2(new A(*p)); cout << "判断目标是否是原始数据:" << ptr2.owner_before(shared_ptr<A>(new A(*p))) << endl; cout << "共享指针的数量:" << ptr2.use_count() << endl; cout << "通过指针访问成员:" << ptr2.get()->a << endl; cout << "是否是单个:" << ptr2.unique() << endl; }
2.weak_ptr ^
weak_ptr可以指向shared_ptr指针指向的对象内存,却并不拥有该内存。使用weak_ptr成员lock,可以返回其指向内存的一个shared_ptr对象,且在所指对象内存已经无效时,返回指针空值。这在验证shared_ptr智能指针的有效性上会有很大作用。
使用例子 ^
#include <iostream> #include <boost/smart_ptr/shared_ptr.hpp> #include <boost/smart_ptr/weak_ptr.hpp> using namespace boost; using std::cout; using std::endl; struct A { int a = 10; }; int main() { A *p = new A; shared_ptr<A> ptr(new A(*p)); shared_ptr<A> ptr1(ptr); weak_ptr<A> pp = ptr; cout << "共享同一个资源的指针数目:" << pp.use_count() << endl; cout << "给定对象是否是原始数据:" << pp.owner_before(shared_ptr<A>(new A(*p))) << endl; if (pp.lock() == nullptr) cout << "所指的对象内存已经无效(调用reset之前)" << endl; ptr.reset(); ptr1.reset(); if (pp.lock() == nullptr) cout << "所指的对象内存已经无效(调用reset之后)" << endl; if (pp.expired()) cout << "所指的对象内存已无效,等同于lock()==nullptr" << endl; pp.reset(); if (pp._empty()) cout << "weak_ptr自身已指向nullptr" << endl; }
3.scoped_ptr ^
scoped_ptr包装了new操作符在堆上分配的动态对象,能够保证动态创建的对象在任何时候都可以被正确地删除。scoped_ptr的所有权很严格,不能转让,相当于C++11中的std::unique_ptr智能指针。又或者说std::unique_ptr的前身就是scoped_ptr。
使用例子 ^
#include <iostream> #include <boost/smart_ptr/scoped_ptr.hpp> using namespace boost; using std::cout; using std::endl; struct A { int a = 10; }; int main() { std::unique_ptr<A> p(new A); scoped_ptr<A> pt(new A); //编译失败,不能使用拷贝构造函数 //scoped_ptr<A> pt1 = pt; cout << "访问目标对象的数据成员:"<<pt.get()->a << endl; pt.reset(); //两个scoped_ptr指针互相交换管理权限 //pt.swap(scoped_ptr对象) }
4.shared_array与scoped_array ^
shared_array与scoped_array时shared_ptr和scoped_ptr的数组版本。用于操作数组的,大部分操作都一样
使用例子 ^
#include <iostream> #include <boost/smart_ptr/shared_array.hpp> #include <boost/smart_ptr/scoped_array.hpp> using namespace boost; using std::cout; using std::endl; int main() { int *a = new int[10]{ 2,4,6,8,10,12,14,16,18,20 }; int *b = new int[10]{ 2,4,6,8,10,12,14,16,18,20 }; shared_array<int> ptr(a); //shared_array指针访问数组元素 for (int i = 0; i != 10; ++i) cout << ptr.get()[i] << " "; cout << endl; //scoped_array指针访问数组元素 scoped_array<int> p(b); for (int i = 0; i != 10; ++i) cout << p.get()[i] << " "; }
C++11中的unique_ptr智能指针相当于scoped_ptr,另shared_ptr和weak_ptr智能指针也跟boost库中的类似,不过C++11标准库中的智能指针不能对数组进行访问。
阅读更多相关文章推荐
- 【02】 Boost库学习笔记之智能指针(Smart_ptr)
- Boost库学习随记三 smart_ptr库、soped_ptr、scoped_array、shared_ptr、shared_array、weak_ptr等等
- C++ boost库学习之——Smart Pointers
- c++ Boost库之boost::bind学习
- C++ 智能指针学习 auto_ptr, unique_ptr 。。。
- C++:Boost库智能指针_shared_ptr
- C++ Standard Stl -- SGI STL源码学习笔记(01) auto_ptr
- C/C++ 日常学习总结(第二十一篇)智能指针shared_ptr
- C++“准”标准库Boost学习指南(1):智能指针Boost.smart_ptr
- 初探boost之smart_ptr库学习笔记
- boost学习: smart-ptr
- C++单例模式实现的坑 -- 学习一下boost库的单例模式
- C++学习 boost学习之-intrusive_ptr
- Boost库学习—智能指针Shared_ptr学习01
- c++ Boost库之boost::bind学习
- C++学习之智能指针--auto_ptr、scoped_ptr、scoped_array、shared_ptr、shared_array、weak_ptr
- C++学习 boost学习之-weak_ptr
- C++ Boost库学习
- C++学习 std::tr1::shared_ptr使用的一点体会tr1库介绍
- [boost库的学习]shared_ptr