BOOST库 之 智能指针
来源:互联网 发布:太极循环人工智能 编辑:程序博客网 时间:2024/06/03 18:20
六大智能指针
scoped_ptr、scoped_array
shared_ptr、shared_array
weak_ptr、intrusive_ptr
//--------------------------
一、scoped_ptr
//--------------------------
1、概述:
指针的所有权不能转让,即:不能拷贝、赋值
【其把拷贝构造函数和赋值操作都声明为私有的,禁止对智能指针的复制操作,保证了
被它管理的指针不能被转让所有权。】
2、包含
#include <boost/smart_ptr.hpp>
3、使用
boost::scoped_ptr<std::string> sp( new std::string("text") );
//--------------------------
二、scoped_array
//--------------------------
1、概述:
指针的所有权不能转让,即:不能拷贝、赋值,管理的是动态数组
2、使用:
boost::scoped_array<int> sa(new int[100]);
int *arr = new int[100];
boost::scoped_array<int> sarr(arr);
sa[0] = 10;
//*(sa + 1) = 20; //错误,scoped_array不提供指针运算,它用起来像普通数组
不推荐使用,使用动态数组时,可使用std::vector
#include <vector>
std::vector<int> varr(100,5);
varr[10] = varr[20] + varr[30];
//--------------------------
三、shared_ptr
//--------------------------
1、概述:
可被自由的拷贝和赋值,它实现的是引用计数型的智能指针,可被安全共享
2、检查引用计数
unique();在shared_ptr是指针的唯一所有者时返回ture.【可靠、高效】
use_count();返回当前指针的引用计数.【仅用测试或调试,有时不可靠】
boost::shared_ptr<int> sptr( new int(10) );
assert( sptr.unique() );
3、支持多态时 指针类型转换
基类->子类 ; 子类->基类
//TODO:
static_pointer_cast<T>() 、 const_pointer_cast<T>()
dynamic_pointer_cast<T>()
扩展:标准的转型操作符
static_cast<T>() 、 const_cast<T>() 、 dynamic_cast<T>()
4、工厂函数 -- 消除显示的new调用
#include <boost/make_shared.hpp>
#include <vector>
boost::shared_ptr<std::string> ssptr = boost::make_shared<std::string>( "Hello" );
boost::shared_ptr<std::vector<int>> vsptr = boost::make_shared<std::vector<int>>( 10, 2);
//--------------------------
四、shared_array
//--------------------------
1、摘要
管理动态数组,可自由拷贝和赋值
int * p = new int[100];
boost::shared_array<int> sarray(p);
boost::shared_array<int> sarray2 = sarray;
sarray[0] = 10;
不推荐使用,可使用 shared_ptr<std::vector> 或 std::vector<shared_ptr> 代替
//--------------------------
五、weak_ptr
//--------------------------
1、说明:
配合shared_ptr而引入,更像是shared_ptr的助手,不具有普通指针的行为,因没有重载oprator * 和 ->
像旁观者观察资源的使用情况。
2、用法:
只是获得资源的观测权,但它没有共享资源,不会引起指针计数的增加或减少
函数lock();从被观测的shared_ptr获得一个可用的shared_ptr对象,从而操作资源去修改资源,引用计数加1
3、获得this指针的shared_ptr
#include <boost/enable_shared_from_this.hpp>
使用的时候只需要让想被shared_ptr管理的类从 enable_shared_from_this<T> 继承即可,
成员函数 shared_from_this()会返回this的shared_ptr.
什么时候使用
在类的内部获得自己的shared_ptr,防止回调的时候之前在callback里边所绑定的指针已失效.
//--------------------------
六、intrusive_ptr
//--------------------------
1、摘要
是一个侵入式的引用计数型指针,用于以下两种情形:
1>对内存占用的要求非常严格,要求必须与原始指针一样;
2>现存代码已经有了引用计数机制管理的对象.
不推荐使用.
scoped_ptr、scoped_array
shared_ptr、shared_array
weak_ptr、intrusive_ptr
//--------------------------
一、scoped_ptr
//--------------------------
1、概述:
指针的所有权不能转让,即:不能拷贝、赋值
【其把拷贝构造函数和赋值操作都声明为私有的,禁止对智能指针的复制操作,保证了
被它管理的指针不能被转让所有权。】
2、包含
#include <boost/smart_ptr.hpp>
3、使用
boost::scoped_ptr<std::string> sp( new std::string("text") );
//--------------------------
二、scoped_array
//--------------------------
1、概述:
指针的所有权不能转让,即:不能拷贝、赋值,管理的是动态数组
2、使用:
boost::scoped_array<int> sa(new int[100]);
int *arr = new int[100];
boost::scoped_array<int> sarr(arr);
sa[0] = 10;
//*(sa + 1) = 20; //错误,scoped_array不提供指针运算,它用起来像普通数组
不推荐使用,使用动态数组时,可使用std::vector
#include <vector>
std::vector<int> varr(100,5);
varr[10] = varr[20] + varr[30];
//--------------------------
三、shared_ptr
//--------------------------
1、概述:
可被自由的拷贝和赋值,它实现的是引用计数型的智能指针,可被安全共享
2、检查引用计数
unique();在shared_ptr是指针的唯一所有者时返回ture.【可靠、高效】
use_count();返回当前指针的引用计数.【仅用测试或调试,有时不可靠】
boost::shared_ptr<int> sptr( new int(10) );
assert( sptr.unique() );
3、支持多态时 指针类型转换
基类->子类 ; 子类->基类
//TODO:
static_pointer_cast<T>() 、 const_pointer_cast<T>()
dynamic_pointer_cast<T>()
扩展:标准的转型操作符
static_cast<T>() 、 const_cast<T>() 、 dynamic_cast<T>()
4、工厂函数 -- 消除显示的new调用
#include <boost/make_shared.hpp>
#include <vector>
boost::shared_ptr<std::string> ssptr = boost::make_shared<std::string>( "Hello" );
boost::shared_ptr<std::vector<int>> vsptr = boost::make_shared<std::vector<int>>( 10, 2);
//--------------------------
四、shared_array
//--------------------------
1、摘要
管理动态数组,可自由拷贝和赋值
int * p = new int[100];
boost::shared_array<int> sarray(p);
boost::shared_array<int> sarray2 = sarray;
sarray[0] = 10;
不推荐使用,可使用 shared_ptr<std::vector> 或 std::vector<shared_ptr> 代替
//--------------------------
五、weak_ptr
//--------------------------
1、说明:
配合shared_ptr而引入,更像是shared_ptr的助手,不具有普通指针的行为,因没有重载oprator * 和 ->
像旁观者观察资源的使用情况。
2、用法:
只是获得资源的观测权,但它没有共享资源,不会引起指针计数的增加或减少
函数lock();从被观测的shared_ptr获得一个可用的shared_ptr对象,从而操作资源去修改资源,引用计数加1
3、获得this指针的shared_ptr
#include <boost/enable_shared_from_this.hpp>
使用的时候只需要让想被shared_ptr管理的类从 enable_shared_from_this<T> 继承即可,
成员函数 shared_from_this()会返回this的shared_ptr.
什么时候使用
在类的内部获得自己的shared_ptr,防止回调的时候之前在callback里边所绑定的指针已失效.
//--------------------------
六、intrusive_ptr
//--------------------------
1、摘要
是一个侵入式的引用计数型指针,用于以下两种情形:
1>对内存占用的要求非常严格,要求必须与原始指针一样;
2>现存代码已经有了引用计数机制管理的对象.
不推荐使用.
阅读全文
0 0
- BOOST库 之 智能指针
- Boost之智能指针
- boost 之 智能指针
- Boost之智能指针
- boost库之shared_ptr库,智能指针
- boost::smart_ptr之智能指针
- 智能指针之boost::weak_ptr
- Boost智能指针之scoped_ptr
- Boost库智能指针学习
- boost库的智能指针
- Boost库智能指针学习
- 【Boost】boost库中智能指针概述
- C++基础之boost库的智能指针
- C++基础之boost库的智能指针
- 【02】 Boost库学习笔记之智能指针(Smart_ptr)
- 浅析boost之智能指针—shared_ptr
- boost智能指针之shared_ptr和weak_ptr
- boost之路 二 智能指针
- KEIL declared implicitly 警告问题
- Hdoj2015_偶数求和
- Calendar
- 关于hash的那点事儿
- 继承,重载,包装
- BOOST库 之 智能指针
- [3] UI原型设计工具Pencil Project 学习系列----- 进阶
- Hdoj2016_数据的交换输出
- UVA 10935
- 进程间通信之共享内存
- gitlab安装配置学习
- Hdoj2017_字符串统计
- Alpine Linux虚拟机安装过程
- Numpy中的广播(Broadcasting)