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

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. Ubuntu16.04安装印象笔记

    Nixnote 是一个 Evernote 开源客户端,原名 Nevernote.Evernote 是一个著名的笔记等个人资料整理和同步软件, 因为 Evernote 没有 Linux 下的官方版本,因 ...

  2. asp.net Mvc 使用uploadify 上传文件 HTTP 302 Error

    CSHTML代码 @{ if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) { <input type=" ...

  3. CSS Backgrounds(背景)

    CSS Backgrounds(背景) CSS 背景属性用于定义HTML元素的背景. CSS 属性定义背景效果: background-color background-image backgroun ...

  4. MVC readioButtonList的创作过程及运用

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Li ...

  5. mysql-community-server安装完后不知道root密码

    修改方法: service mysqld stop mysqld_safe --skip-grant-tables & mysql -u root use mysql; update user ...

  6. 20145333《Java程序设计》第3次实验报告

    20145333<Java程序设计>第3次实验报告 实验内容 使用 git 上传代码 使用 git 相互更改代码 实现代码的重载 实验步骤 git设置用户名邮箱,ssh公钥 用git上传代 ...

  7. Vjudge - E - 这是高中数学向量题

    2017-07-15 22:29:06 writer:pprp 评价,用到了叉乘,很麻烦,C++构造知识必须扎实 题目如下: 我们用逆时针方向的顶点序列来表示,我们很想了解这块地的基本情况,现在请你编 ...

  8. nginx web服务器详解1(转)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://freeloda.blog.51cto.com/2033581/1285332 大 ...

  9. HttpServletRequest request方法详解

    //1.获取请求参数 //获取参数的单个值,如有多个则只返回第一个 String parameter1 = request.getParameter("demo"); //获取参数 ...

  10. git tags 管理

    新建标签: git tag -a V1.1 -m "some thing" (新建标签前请先commit代码) 推送标签: git push --tags (推送标签前请先推送代码 ...