1. 介绍

  shared_ptr 是通过指针保持某个对象的共享拥有权的智能指针。

若干个 shared_ptr 对象能够拥有同一个对象;最后一个指向该对象的 shared_ptr 被销毁或重置时。该对象被销毁。销毁该对象时使用的是 delete 表达式或者是在构造 shared_ptr 时传入的自己定义删除器(deleter)。

  特点:

  

  • shared_ptr 也能够不拥有对象。称作空(empty)。
  • 最后一个shared_ptr指针被删除时,对象才被删除。

  • shared_ptr 持有的指针是通过 get() 返回的;而控制块所持有的指针/对象则是终于引用计数归零时会被删除的那个。两者并不一定相等。
  • shared_ptr 的析构函数会将控制块中的 shared_ptr 计数器减一,假设减至零。控制块就会调用被管理对象的析构函数。

    但控制块本身直到 std::weak_ptr 计数器相同归零时才会释放。

    在std,std::tr1和boost中都含有这个智能指针。差别能够看以下这段话:

1 - std::bind is the the standard name for it. This will be the name you use for C++11 compliant libraries. List of all libraries in standardized C++.

2 - std::tr1::bind is C++ Technical Report 1 namespace. Between C++03 and C++11 there was the C++ Technical Report 1, which proposed additional libraries and enhancements. Most of these already existed in Boost at the time, and some of these library changes were adopted in the C++11 standard, like and (which contains std::bind). The std::tr1 namespace was used to differentiate the libraries in their work-in-progress state, as opposed to everything standardized in the std namespace.

3 - boost::bind is for bind in the boost namespace, if you are using the Boost library. Boost encompasses much more than what is in TR1 and what i in C++11’s std library. List of all libraries in Boost as of 1.52.0

Most of what was in TR1 has been standardized and is in the C++11 std namespace, and C++11 contains more libraries than mentioned in TR1 that were adapted from Boost constructs, like threading support defined in .

Part of what defines what you can use and which namespace you can use now depends on your compiler. I don’t recall, but I think the more recent GCC-g++ implementations have started using std namespaces for the new C++11 libraries, but might require a different compiler flag to activate that. They will still support the std::tr1 namespace though. Visual C++ 2010 moved what was previously in std::tr1 into the normal std namespace, but Visual C++ 2008 still used std::tr1.


2. shared_ptr使用

  正确合理的使用shared_ptr智能指针能够防止内存泄露,以下通过一段代码就能够非常好地说明问题。

  

#include <stdio.h>
#include <iostream>
#include <tr1/memory>
#include <thread>
#include <chrono>
#include <mutex> class A{
public:
A(){
std::cout<<"Construct A!"<<std::endl;
};
~A(){
std::cout<<"Destruct A!"<<std::endl;
};
}; class B: public A {
public:
B(){
std::cout<<"Construct B!"<<std::endl;
};
~B(){
std::cout<<"Destruct B!"<<std::endl;
};
}; int main(){
B *b1 = new B();
std::cout<<"-----------divid line--------"<<std::endl;
std::tr1::shared_ptr<B> b2(new B()); return 0;
}

  A是基类,B继承于A。通过B *b1 = new B()定义一个B类的对象。观察其构造和析构过程。然后再通过shared_ptr定义B的对象,观察构造和析构过程。结果例如以下:

  

Construct A!

Construct B!

———–divid line——–

Construct A!

Construct B!

Destruct B!

Destruct A!

  结果已经非常能说明问题了。!!

最新文章

  1. jQuery入门教程
  2. 在windows下新建maven项目
  3. App跳转至系统Settings
  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充
  5. WebBrowser控件默认使用IE9,IE10的方法
  6. Symfony2 EventDispatcher组件
  7. js继承模式
  8. pxe网络安装操作系统 原理与详细过程
  9. Sql Server 数据库表结构,存储过程,视图比较脚本
  10. GetSystemInfo()
  11. xslfo和fop使用中的一些问题
  12. E - 着色方案 HYSBZ - 1079 (计数DP)
  13. CVPR2013总结
  14. Django实现cookie&amp;session以及认证系统
  15. unity编辑器之自动提示订外卖
  16. Hadoop 2.6.0 HIVE 2.1.1配置
  17. shell脚本----if(数字条件,字符串条件,字符串为空)
  18. 【黑金原创教程】 FPGA那些事儿《概念篇》
  19. Django框架 之 admin管理工具(源码解析)
  20. [LOJ6279]数列分块入门 3

热门文章

  1. 关闭 macOS Google Chrome 黑暗模式风格
  2. Linux vsftpd服务
  3. Release Python Program as exe
  4. 搭建Mysql主从复制
  5. 【7.1.1】ELK日志系统单体搭建
  6. Google JavaScript代码风格指南
  7. 大数据学习——hive安装部署
  8. @setupmethod -- flask.setupmethod
  9. [luoguP2513] [HAOI2009]逆序对数列(DP)
  10. hdu1160简单dp最长下降子序列