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::shared_ptr

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

  2. std::shared_ptr

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

  3. (译+原)std::shared_ptr及std::unique_ptr使用数组

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

  4. std::shared_ptr<void>的工作原理

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

  5. 巧用std::shared_ptr全局对象释放单例内存

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

  6. Item 20: 使用std::weak_ptr替换会造成指针悬挂的类std::shared_ptr指针

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

  7. std::shared_ptr 和普通指针的转换

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

  8. C++11 std::shared_ptr总结与使用

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

  9. std::shared_ptr之deleter的巧妙应用

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

随机推荐

  1. 15SpringMvc_在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型

    之前第12篇文章中提到过在业务控制方法中写入普通变量收集参数的方式,也提到了这种凡方式的弊端(参数很多怎么办),所以这篇文章讲的是在业务控制方法中写入模型变量来收集参数.本文的案例实现的功能是,在注册 ...

  2. GitLab/Git在AndroidStudio上的使用(转)

    1.在AndroidStudio上的配置GitLab 1)首先先将gitlab上的开发项目clone到本地(可以使用命令行或者管理工具,具体操作在GitLab中已经涉及,这里不再赘述),然后导入到An ...

  3. 【每日学习】Apache重写未开启,导致The requested URL /xxxx.html was not found on this server

    今天把项目环境从集成换成独立的,全部搭建好后,网站主页www.xxx.com能打开,但一涉及到跳转,带参数,比如 www.xxx.com/xxx/xxx.html 就会报错 The requested ...

  4. (OAF)jdeveloper集成log4j并将日志输出到指定文件并写入数据库

    参考: How to configure Log4j in JDev 11g Ever wanted to use log4j in your adf project ? Well though Or ...

  5. Google java代码风格导入Eclipse

    Git地址 https://github.com/codeset/google-java-styleguide 下载配置文件在Eclipse中执行导入:Window -> Preferences ...

  6. Java系列:Add Microsoft SQL JDBC driver to Maven

    Maven does not directly support some libraries, like Microsoft's SQL Server JDBC. This tutorial will ...

  7. [CareerCup] 14.5 Object Reflection 对象反射

    14.5 Explain what object reflection is in Java and why it is useful. Java中的对象反射机制可以获得Java类和对象的反射信息,并 ...

  8. Maven实战之antrun插件

    在 Maven实际使用过程中,有时候在对一些旧有的项目的做从Makefile和ant到Maven迁移时需要对一些步骤做特殊处理,比如说编译JNI代 码,虽然Maven有个native插件可以用,但需要 ...

  9. 海量数据处理利器之Hash——在线邮件地址过滤

    标题用了了海量数据(Massive datasets)而不用大数据(Big data).感觉大数据还是略微有点虚,来点实际的. 一.需求 现在我们需要设计一个在线过滤垃圾邮件地址的方案,我们的数据库里 ...

  10. maven integration with eclipse 3.0.4 does not work with NTLM proxy

    Recently downloaded m2e(maven integration with eclipse). The version is 3.0.4. My environment is beh ...