C++11_shared_ptr
版权声明:本文为博主原创文章,未经博主允许不得转载。
shared_ptr智能指针的一种,它的使用类似于auto_ptr.
shared_ptr它有两个指针,一个指向引用计数,一个指向data.由于拥有引用计数,所有shared_ptr支持容器.
shared_ptr的源码非常复杂这里也不做太多讲解,本文主要介绍一下shared_ptr的基本使用
间接使用
#include <iostream> using namespace std; class Person
{
public:
Person()
{
cout<< "ctor" << endl;
}
~Person()
{
cout<< "dctor" << endl;
} int m_age = ;
}; int main()
{
shared_ptr<Person> person(new Person());
cout << (*person).m_age<< endl;
cout << "引用计数: " << person.use_count() << endl; // use_count()返回几个该对象被几个人拥有 return ;
}
输出结果

直接使用
int main()
{
//赋值操作
int *p = new int ();
int *q = new int (); shared_ptr<int> tmp_5(p);
shared_ptr<int> tmp_10(q); tmp_5 = tmp_10;
cout<< * tmp_5 << endl; return ; }
输出结果

检测拥有者
int main()
{
//unique() 是否为最初拥有者
Person * p = new Person; shared_ptr<Person> tmp_1(p);
cout <<"被拥有个数"<< tmp_1.use_count() <<"是否为最初拥有者"<<tmp_1.unique()<< endl; shared_ptr<Person> tmp_2(tmp_1);
cout <<"被拥有个数"<< tmp_2.use_count() <<"是否为最初拥有者"<<tmp_2.unique()<< endl; return ;
}
输出结果

以上是shared_ptr<>的简单使用
下面我来介绍一种类的设计方式Hand/Body(pImpl)
这边设计方式其实就是桥接设计模式,但是个人感觉这种设计类的方式是未来的趋向
他的观点如图

(handle) fileview类只含有一个指针(4字节),指向真正的数据
(body) implementation, 数据 , 方法实现
这样做 handle 内存占用大小会减小很多,并且他有助于move()的实现
此时调用fileview的function时就会调用 implementation内的实现
写一个简单的实现
#include <iostream>
#include <vector>
#include "/Users/oujiken/Qt5.6.1/5.6/Src/qt3d/src/3rdparty/assimp/code/BoostWorkaround/boost/shared_ptr.hpp"
using namespace std; class fileview
{
public:
fileview();
~fileview();
void fileview_close();
...
private:
struct implementation;
boost::shared_ptr<implementation> pimpl;
}; struct fileview::implementation
{
std::vector<char> m_data;
}; //ctor
fileview::fileview()
{
pimpl.reset(new implementation());
cout<< "fileview()" <<endl;
} void fileview::fileview_close()
{
pimpl->m_data.clear();
cout<< "close()"<<endl;
}
...
int main()
{
fileview * f = new fileview();
f->fileview_close();
cout << sizeof(fileview) <<endl;
return ;
}
输出结果

shared_ptr<> 占16字节
如有不正确的地方请指正
参照<<侯捷 C++新标准 C++11>>
C++11_shared_ptr的更多相关文章
随机推荐
- hive union all使用注意
UNION用于联合多个select语句的结果集,合并为一个独立的结果集,结果集去重. UNION ALL也是用于联合多个select语句的结果集.但是不能消除重复行.现在hive只支持UNION AL ...
- GridView自定义自增长的 序号 列
如图所示,添加一个普通列(非模板列),将其显示文本为 序号 在GridView的RowDataBound事件中作如下处理 后台.CS 代码:
- oracle错误一览表
ORA-00001: 违反唯一约束条件 (.)ORA-00017: 请求会话以设置跟踪事件ORA-00018: 超出最大会话数ORA-00019: 超出最大会话许可数ORA-00020: 超出最大进程 ...
- JavaScript常用算法
一.排序算法 1.Array.sort(function)(JavaScript原生排序算法)参数:比较函数(可选)若无参数,则按照首字母的ASCII码排序,比较函数的作用为确定排序 function ...
- 单调队列:temperature
题目大意:某国进行了连续n天的温度测量,测量存在误差,测量结果是第i天温度在[l_i,r_i]范围内. 求最长的连续的一段,满足该段内可能温度不降. 第一行n下面n行,每行l_i,r_i 1<= ...
- tsar的使用
项目地址https://github.com/alibaba/tsar 安装 $ git clone git://github.com/kongjian/tsar.git $ cd tsar $ ma ...
- PyTorch源码解读之torchvision.transforms(转)
原文地址:https://blog.csdn.net/u014380165/article/details/79167753 版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...
- 随机数的生成 - rand(), srand()
2017-08-20 17:43:29 writer:pprp 我们采用随机数可以对我们的算法进行大数据检验 /* name : 简单的随机数生成算法 writer : pprp declare : ...
- 01_Storm体系概要
1. Storm发展历史 Storm历史 1. 2010年12月,backtype公司Nathan,提出Storm的核心概念2. backtype, 提供数据分析,数据处理服务的一个公司3. 2011 ...
- PHP整数取余返回负数的相关解决方法
PHP语言虽然功能强大,但并不代表其没有缺点,在编写代码的过程中未免会遇到一些让人头痛的问题.下面我们将为大家介绍有关PHP整数取余返回负数的解决办法. 我们先来看个例子. $res = 162447 ...