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智能指针能够防止内存泄露,以下通过一段代码就能够非常好地说明问题。

  

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <tr1/memory>
  4. #include <thread>
  5. #include <chrono>
  6. #include <mutex>
  7. class A{
  8. public:
  9. A(){
  10. std::cout<<"Construct A!"<<std::endl;
  11. };
  12. ~A(){
  13. std::cout<<"Destruct A!"<<std::endl;
  14. };
  15. };
  16. class B: public A {
  17. public:
  18. B(){
  19. std::cout<<"Construct B!"<<std::endl;
  20. };
  21. ~B(){
  22. std::cout<<"Destruct B!"<<std::endl;
  23. };
  24. };
  25. int main(){
  26. B *b1 = new B();
  27. std::cout<<"-----------divid line--------"<<std::endl;
  28. std::tr1::shared_ptr<B> b2(new B());
  29. return 0;
  30. }

  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!

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

C++ std::tr1::shared_ptr使用说明的更多相关文章

  1. c++智能指针《二》 std::tr1::shared_ptr

    转载http://www.cnblogs.com/kadinzhu/archive/2011/12/12/2284826.html 看<effective c++>,作者一直强调用std: ...

  2. 智能指针tr1::shared_ptr、boost::shared_ptr使用

    对于tr1::shared_ptr在安装vs同一时候会自带安装,可是版本号较低的不存在.而boost作为tr1的实现品,包括 "Algorithms Broken Compiler Work ...

  3. std::tr1::function

    转自:https://www.cnblogs.com/qlee/archive/2011/07/04/2097594.html 在C++的TR1中(Technology Report)中包含一个fun ...

  4. C++ std::tr1::bind使用

    1. 简述 同function函数相似.bind函数相同也能够实现相似于函数指针的功能.但却却比函数指针更加灵活.特别是函数指向类 的非静态成员函数时.std::tr1::function 能够对静态 ...

  5. std::tr1::function和bind组件

    C++中std::tr1::function和bind 组件的使用 在C++的TR1中(Technology Report)中包含一个function模板类和bind模板函数,使用它们可以实现类似函数 ...

  6. std::bind 的使用说明

    转自: https://www.cnblogs.com/cmranger/p/4743926.html ///////////////////// std::bind bind是对C++98标准中函数 ...

  7. std::function 的使用说明

    转自: https://www.cnblogs.com/heartchord/p/5017071.html //////////////////// std::function   参考资料 • cp ...

  8. gtest 1.7编译错误:std:tr1:tuple模板参数过多的解决方案

    在gtest/gtest.h文件中添加如下代码 #define _VARIADIC_MAX 10

  9. auto_ptr,shared_ptr 智能指针的使用

    Q: 那个auto_ptr是什么东东啊?为什么没有auto_array?A: 哦,auto_ptr是一个很简单的资源封装类,是在<memory>头文件中定义的.它使用“资源分配即初始化”技 ...

随机推荐

  1. linux(php环境) 安装ffmpeg

    实现上传视频获取视频的第一帧当做视频封面 1.安装ffmpeg ffmpeg的下载链接  https://ffmpeg.org/download.html 解压安装包 tar -jxvf ffmpeg ...

  2. java中的编译时与运行时

    ----?基础知识   -- 编译时 编译器将源代码翻译成机器能够读懂的代码,如java中就是翻译成jvm能够读懂的字节码文件.简单说,编译时就是机器帮我们检查代码是否有出现语法错误,关键字写错之类的 ...

  3. Linux基础学习-RHEL7.4之YUM更换CentOS源

    1.配置YUM本地源 1.挂载镜像 [root@qdlinux ~]# mount /dev/cdrom /mnt 2.查看是否挂载成功 [root@qdlinux ~]# df -h Filesys ...

  4. python-----定制群发微信消息

    如何使用表格中的信息群发微信消息? 如何读取csv? →   使用内置模块csv 如何按对应信息发送到微信?→  使用第三方库wxpy 以下代码素材自取:链接:https://pan.baidu.co ...

  5. NGINX模块(二)

    [Nginx标准HTTP模块] 一.HTTP核心模块 指令1:alias 语法:alias file-path|directory-path; 默认值:no 使用字段:location 说明:这个指令 ...

  6. 利用Bitvise SSH Client设置二级代理

    浏览器设置代理 chrome: 插件:SwitchyOmega 二级代理 软件:Bitvise SSH Client 友情连接:链接: https://pan.baidu.com/s/1fdth_TZ ...

  7. jenkins在linux环境搭建-公司系统

    1.按照这个搭建的项目https://www.cnblogs.com/zishengY/p/7170656.html 2.配置权限https://blog.csdn.net/mynameissls/a ...

  8. Selenium学习系列---- FirePath的安装和使用

    在用Selenium编写测试用例的时候,需要对对网页元素上定位,而现在很多的浏览器是可以看到网页上相关的元素信息,可以查看某一个网页的元素信息,通过定位的方式查找元素.另外安装好Selenium ID ...

  9. bzoj3875 【Ahoi2014】骑士游戏 spfa处理后效性动规

    骑士游戏 [故事背景] 长期的宅男生活中,JYY又挖掘出了一款RPG游戏.在这个游戏中JYY会 扮演一个英勇的骑士,用他手中的长剑去杀死入侵村庄的怪兽. [问题描述] 在这个游戏中,JYY一共有两种攻 ...

  10. PHP字符串的替换(preg_replace)

    /* 正则表达式  preg_replace() */ $str = array( "如果没有一些http://www.abc.com特殊的<b>替换</b>需5求( ...