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!
结果已经非常能说明问题了。!!
C++ std::tr1::shared_ptr使用说明的更多相关文章
- c++智能指针《二》 std::tr1::shared_ptr
转载http://www.cnblogs.com/kadinzhu/archive/2011/12/12/2284826.html 看<effective c++>,作者一直强调用std: ...
- 智能指针tr1::shared_ptr、boost::shared_ptr使用
对于tr1::shared_ptr在安装vs同一时候会自带安装,可是版本号较低的不存在.而boost作为tr1的实现品,包括 "Algorithms Broken Compiler Work ...
- std::tr1::function
转自:https://www.cnblogs.com/qlee/archive/2011/07/04/2097594.html 在C++的TR1中(Technology Report)中包含一个fun ...
- C++ std::tr1::bind使用
1. 简述 同function函数相似.bind函数相同也能够实现相似于函数指针的功能.但却却比函数指针更加灵活.特别是函数指向类 的非静态成员函数时.std::tr1::function 能够对静态 ...
- std::tr1::function和bind组件
C++中std::tr1::function和bind 组件的使用 在C++的TR1中(Technology Report)中包含一个function模板类和bind模板函数,使用它们可以实现类似函数 ...
- std::bind 的使用说明
转自: https://www.cnblogs.com/cmranger/p/4743926.html ///////////////////// std::bind bind是对C++98标准中函数 ...
- std::function 的使用说明
转自: https://www.cnblogs.com/heartchord/p/5017071.html //////////////////// std::function 参考资料 • cp ...
- gtest 1.7编译错误:std:tr1:tuple模板参数过多的解决方案
在gtest/gtest.h文件中添加如下代码 #define _VARIADIC_MAX 10
- auto_ptr,shared_ptr 智能指针的使用
Q: 那个auto_ptr是什么东东啊?为什么没有auto_array?A: 哦,auto_ptr是一个很简单的资源封装类,是在<memory>头文件中定义的.它使用“资源分配即初始化”技 ...
随机推荐
- 【2019.5.19】接口测试及python基础(一)
一.接口 1.什么是接口: 简单的说,接口就是从数据库获取数据. 2.前端和后端: 2.1前端client: 对于web来说,打开的网页,我们所看到的就是前端,前端语言包括html.JS.CSS: ...
- C++位域和内存对齐问题
1. 位域: 1. 在C中,位域可以写成这样(注:位域的数据类型一律用无符号的,纪律性). struct bitmap { unsigned a : ; unsigned b : ; unsigned ...
- ftp上传文件、删除文件、下载文件的操作
FavFTPUtil.Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- qt c++对象头文件如何相互包含
今天在写qt时,遇到了两个类相互包含的问题,类A要用到类B,类B要用到类A. 类A:a.h #ifndef A_H #define A_H #include <b.h> class A { ...
- Centos6.5安装Nexus及安装时的一些错误
注意:此篇博文未有配置部分,有需求的同学只能自行寻找了-- 1.下载: https://www.sonatype.com/download-oss-sonatype 2.官方推荐安装在/opt目录下 ...
- Android GradientDrawable的XML实现
Android GradientDrawable的XML实现 Android GradientDrawable与附录文章1类似,这次以XML而非Java代码形式实现.比如写好一个shape文件放 ...
- [luoguP2886] [USACO07NOV]牛继电器Cow Relays(矩阵)
传送门 矩阵快速幂,本质是floyd 把 * 改成 + 即可 注意初始化 因为只有100条边,所以可以离散化 #include <cstdio> #include <cstring& ...
- 如何在Eclipse中生成Native类对应的JNI的.h文件
1 致谢 感谢super_level网友 他的博客写的很清楚 给了我很多帮助 链接如下:http://blog.csdn.net/super_level/article/details/2124353 ...
- centos 6.5 yum安装lnmp
转自:http://blog.csdn.net/lane_l/article/details/20235909 准备篇: 端口 vi /etc/sysconfig/iptables -A INPUT ...
- ASP.NET程序开发中经典常用的三十三种代码实例[确实有用]
原文发布时间为:2008-11-10 -- 来源于本人的百度文章 [由搬家工具导入] ASP.NET程序开发中经典常用的三十三种代码实例:1. 打开新的窗口并传送参数: 传送参数:response.w ...