版权声明:本文为博主原创文章,未经博主允许不得转载。

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的更多相关文章

随机推荐

  1. Java伙伴系统(模拟)

    参考:https://labrick.cc/2015/10/12/buddy-system-algorithm/ 代码过烂 不宜参考. output: [operating.entity.Heap@4 ...

  2. OpenStack之基础知识

    一.云计算 云计算(cloud computing)是基于互联网的相关服务的增加.使用和交付模式,通常涉及通过互联网来提供动态易扩展且经常是虚拟化的资源.云是网络.互联网的一种比喻说法.过去在图中往往 ...

  3. MySQL多版本并发控制机制(MVCC)-源码浅析

    MySQL多版本并发控制机制(MVCC)-源码浅析 前言 作为一个数据库爱好者,自己动手写过简单的SQL解析器以及存储引擎,但感觉还是不够过瘾.<<事务处理-概念与技术>>诚然 ...

  4. elasticsearch 5.x集群安装

    1. 下载 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.tar.gz 2. 解压 为便于 ...

  5. 20145103《JAVA程序设计》第十周学习总结

    网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时,大部分的程序设 ...

  6. 20145307第二次JAVA学习实验报告

    20145307<Java程序设计>实验报告二:Java面向对象程序设计 实验要求 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4 ...

  7. [BZOJ4756]Promotion Counting

    Description The cows have once again tried to form a startup company, failing to remember from past ...

  8. Linux后台运行命令,nohup和&的区别

    &的意思是在后台运行, 什么意思呢?  意思是说, 当你在执行 ./a.out & 的时候, 即使你用ctrl C,  那么a.out照样运行(因为对SIGINT信号免疫). 但是要注 ...

  9. web platform installer

    下载链接 https://www.microsoft.com/web/downloads/platform.aspx 默认的安装路径 C:\Program Files\Microsoft\Web Pl ...

  10. "ImportError: cannot import name OVSLegacyKernelSwitch"

    My VirtualMachine OS is Ubuntu14.04, Linux. Recently I want to install the mininet in my OS, and I u ...