std::shared_ptr(二)

时间:2021-08-02 04:13:54
Defined in header <memory>
   
 
template< class T > class shared_ptr;
  (since C++11)
 

std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Severalshared_ptr objects may own the same object; the object is destroyed when the last remaining shared_ptr pointing to it is destroyed or reset. The object is destroyed using delete-expression or a custom deleter that is supplied toshared_ptr during construction.

shared_ptr may also own no objects, in which case it is called empty.

shared_ptr meets the requirements of CopyConstructible and CopyAssignable.

Member types

 
Member type Definition
 
element_type T

Member functions

 
constructs new shared_ptr 
(public member function)
 
destructs the owned object if no more shared_ptrs link to it 
(public member function)
 
assigns the shared_ptr 
(public member function)
Modifiers
 
replaces the managed object 
(public member function)
 
swaps the managed objects 
(public member function)
Observers
 
returns a pointer to the managed object 
(public member function)
 
dereferences pointer to the managed object 
(public member function)
 
returns the number of shared_ptr objects referring to the same managed object 
(public member function)
 
checks whether the managed object is managed only by the current shared_ptr instance 
(public member function)
 
checks if there is associated managed object 
(public member function)
 
provides owner-based ordering of shared pointers 
(public member function)

Non-member functions

 
creates a shared pointer that manages a new object 
(function template)
 
creates a shared pointer that manages a new object allocated using an allocator 
(function template)
 
applies static_castdynamic_cast or const_cast to the type of the managed object 
(function template)
 
returns the deleter of specified type, if owned 
(function template)
 
compares with another shared_ptr or with nullptr 
(function template)
 
outputs the value of the managed pointer to an output stream 
(function template)
 
specializes the std::swap algorithm 
(function template)
 
specializes atomic operations 
(function template)

Helper classes

 
hash support for std::shared_ptr 
(class template specialization)

Implementation notes

In a typical implementation, std::shared_ptr holds only two pointers:

  • a pointer to the referenced object
  • a pointer to control block

Where the control block is a dynamically-allocated object that holds:

  • a pointer to the managed object or the managed object itself
  • the deleter
  • the allocator
  • the number of shared_ptrs that own the managed object
  • the number of weak_ptrs that refer to the managed object

When shared_ptr is created by calling std::make_shared or std::allocate_shared, the control block holds the managed object directly, as a data member. When shared_ptr is created by calling a constructor, a pointer is stored.

The pointer held by the shared_ptr directly is the one returned by get(), while the pointer/object held by the control block is the one that will be deleted when the number of shared owners reaches zero: these pointers are not necessarily equal.

The destructor of shared_ptr decrements the number of shared owners of the control block, and if that reaches zero, the control block calls the destructor of the managed object, but the control block does not deallocate itself until thestd::weak_ptr counter reaches zero as well.

这个360还不让人复制,直接把JS禁掉,以前学的不是白学的

std::shared_ptr(二)的更多相关文章

  1. tolua 转换 std&colon;&colon;shared&lowbar;ptr

    tolua 转换 std::shared_ptr 自从c++11以后std::shared_ptr几乎是比用的东西,经常会遇到类似如下应用 std::shared_ptr<Tst_ShareTe ...

  2. std&colon;&colon;shared&lowbar;ptr

    在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...

  3. (译&plus;原)std&colon;&colon;shared&lowbar;ptr及std&colon;&colon;unique&lowbar;ptr使用数组

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462363.html 参考网址: http://*.com/questions ...

  4. std&colon;&colon;shared&lowbar;ptr&lt&semi;void&gt&semi;的工作原理

    前戏 先抛出两个问题 如果delete一个指针,但是它真实的类型和指针类型不一样会发生什么? 是谁调用了析构函数? 下面这段代码会发生什么有趣的事情? // delete_diff_type.cpp ...

  5. 巧用std&colon;&colon;shared&lowbar;ptr全局对象释放单例内存

    巧用std::shared_ptr 单例的使用相对比较广泛,但是需要在程序退出前调用它的析构函数对数据进行释放,常规做法是在main函数末尾进行释放工作, 但是这样相对比较繁琐,因此便有了利用全局变量 ...

  6. Item 20&colon; 使用std&colon;&colon;weak&lowbar;ptr替换会造成指针悬挂的类std&colon;&colon;shared&lowbar;ptr指针

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 矛盾的是,我们很容易就能创造出一个和std::shared_ptr ...

  7. std&colon;&colon;shared&lowbar;ptr 和普通指针的转换

    相互转化见示例 struct test { int num; string name; }; test* pTest = new test(); std::shared_ptr<test> ...

  8. C&plus;&plus;11 std&colon;&colon;shared&lowbar;ptr总结与使用

    最近看代码,智能指针用的比较多,自己平时用的少,周末自己总结总结.方便后续使用. std::shared_ptr大概总结有以下几点: (1) 智能指针主要的用途就是方便资源的管理,自动释放没有指针引用 ...

  9. std&colon;&colon;shared&lowbar;ptr之deleter的巧妙应用

    本文由作者邹启文授权网易云社区发布. std::shared_ptr 一次创建,多处共享,通过引用计数控制生命周期. 实例 在邮箱大师PC版中,我们在实现搜索时,大致思路是这样的: 每一个账号都有一个 ...

随机推荐

  1. 会话控制&colon;Cookie和session

    HTTP(超文本传输协议)定义了通过万维网(WWW)传输文本.图形.视频和所有其他数据所有的规则.HTTP是一种无状态的协议,说明每次请求的处理都与之前或之后的请求无关.虽然这种简化实现对于HTTP的 ...

  2. JSBinding&plus;Bridge&period;NET:生成绑定(导出)

    将框架代码导出到 JavaScript.就可以在 JavaScript 中调用 框架代码 的功能. 注意,这个功能是在 Js工程中做的,Cs工程没有这回事. 如何导出? 1. 将需要导出的类添加到 J ...

  3. 关于百度地图api测距显示NaN的解决方案

    因为随着百度地图的api的升级,测距的函数以及语句都发生的一定变化. 在调用api测距的时候通常我们使用的是语句map.getDistance(marker1,marker2); 但为什么这么简单的测 ...

  4. gitlab&plus;TortoiseGit中使用SSH

    1.在文件夹空白位置右键打开"Git Bash" 2.按 https://gitlab.yourhost.com/help/ssh/ssh.md 中的说明,输入命令   ssh-k ...

  5. JSP out乱码

    在form method="post" 写post 在接受页面jsp代码前面加request.setCharacterEncoding("UTF-8");

  6. 【ubuntu】install openbox&plus;tint2&plus;bmenu on ubuntu12&period;04&period;4

    原文地址: http://ndever.net/articles/linux/install-openbox-ubuntu-1304-1310 openbox是我用过的轻量窗口中最好用的了. Step ...

  7. ARM&amp&semi;Linux 下驱动开发第三节

    后台驱动代码如下:比较昨天的,添加了读写指针位置移动操作 #include<linux/init.h> #include<linux/module.h> #include&lt ...

  8. SpringMVC &plus; Spring 3&period;2&period;14 &plus; Hibernate 3&period;6&period;10

    SpringMVC + Spring 3.2.14 + Hibernate 3.6.10 集成详解 注:此文档只说明简单的框架集成,各个框架的高级特性未涉及,刚刚接触框架的新人可能需要参考其他资料. ...

  9. wdk1703&plus;vs2015编译的诡异问题

    最近将wdk升级到1703(10.0.15063.0)版本,编译一个新建的minifiter项目居然出现了失败 提示错误为 WindowsDriver.common.targets(460,5): e ...

  10. netty04(重点来了、指定某个客户端发信息或者群发)小声嘀咕~~我也是从零开始学得、、、想学习netty的又不知道怎么下手的童鞋们~~

    还是和上几篇一样,先给出前面笔记的连接,有没看的可以去看看再来! netty01   . netty02  .netty03 看到这里.你基本上可以使用netty接受信息和根据对应的信息返回信息了 接 ...