C++ std::tr1::shared_ptr使用说明
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!
结果已经非常能说明问题了。!!
转载于:https://www.cnblogs.com/yutingliuyl/p/7190899.html
C++ std::tr1::shared_ptr使用说明相关推荐
- 【转】C++ std::tr1::shared_ptr使用
看<effective c++>,作者一直强调用std::tr1::shared_ptr,比起auto_ptr好多了. shared_ptr采用引用计数,多个指针可以指向同一个对象:aut ...
- C++学习 std::tr1::shared_ptr使用的一点体会tr1库介绍
Technical Report 1 是一份规范,描述加入C++标准程序的诸多新技能,以新的class templates 和 function templates 形式体现,针对的题目有哈希表,基于 ...
- std::tr1::shared_ptr点滴记录
因为使用shared_ptr导致的程序异常崩溃问题原因 1.由shared_ptr类的.get()函数导致. 理解(不一定完全跟代码一致,但是便于理解):(见下图)
- C++中std::tr1::function和bind 组件的使用
在C++的TR1中(Technology Report)中包含一个function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类的非静态成员函 ...
- std::tr1::function and std::tr1::bind
std::tr1::function and std::tr1::bind 每日一话 一本正经的胡说八道 引言 在C++的TR1中(Technology Report)中包含一个function模板类 ...
- std::tr1::bind使用帮助
头文件 #include <tr1/functional> 语法糖 #define BIND(func, inst) std::tr1::bind(func, inst, std::tr1 ...
- std::tr1::function
转自:https://www.cnblogs.com/qlee/archive/2011/07/04/2097594.html 在C++的TR1中(Technology Report)中包含一个fun ...
- vc2010 std::tr1 bind库捉虫记
前两天发现了VC2010 tr1库中bind实现的一个bug,当时只是作了记录,没有详细分析.但作为一个QA,不找出问题所在实在不算称职,于是就有了这篇捉虫记. 闲言少叙,书归正传,tr1库就不多作介 ...
- 转:Qt编译 error: ‘std::tr1’ has not been declared
https://blog.csdn.net/baidu_33850454/article/details/79147161 在deepin 64 系统编译Qt(执行make)时报错: In file ...
最新文章
- 几种filter的比较
- 高性能IO -Reactor模式的实现
- java中 indexOf() 与lastIndexOf() 用法详解
- 达摩院文档级关系抽取新SOTA和零样本关系抽取新任务
- 引入Spring集成
- 德国超级计算机中心,德国:强化人工智能能力建设 加大高性能计算网络投资...
- Fedex Ship Manager Software安装
- 【LeetCode笔记】剑指 Offer 14. 剪绳子 I II(Java、动态规划、偏数学)
- 两台服务器之间mysql数据库怎么做同步_mysql数据库占满磁盘导致服务器无法运行...
- 复习:稀疏链表的十字链表
- MTK 驱动开发(26)---背光灯
- 音视频专题--音频剪辑原理
- 崩坏3服务器维护2月8号,崩坏3 8月29日更新官方公告
- MATLAB求解导弹运动的一些基础方法
- 如何提高团队管理能力6
- 来了!Python官方文档中文版
- div内元素不在一行的问题解决方法
- 中石油计算机图形学第二次在线,石油华东《计算机图形学》2020年秋季学期在线作业(二)...
- 黑客教父龚蔚:是谁打开了潘多拉的魔盒
- 思科无线AP配置之一(使用超级终端连接设备篇)