auto_ptr与shared_ptr
注: 从c++11开始, auto_ptr已经被标记为弃用, 常见的替代品为shared_ptr
shared_ptr的不同之处在于引用计数, 在复制(或赋值)时不会像auto_ptr那样直接转移所有权
auto_ptr
- auto_ptr实际也是一种类, 拥有自己的析构函数, 生命周期结束时能自动释放资源
- 正因为能自动释放资源, 特别适合在单个函数内代替new/delete的调用, 不用自己调用delete也不用担心意外退出
- auto_ptr对资源的拥有者只能有一个, 当两个auto_ptr进行等于号(赋值)操作时, 等于号后面的auto_ptr将失去资源的所有权
- 如果只是想显示auto_ptr对应资源的信息, 而不是更改的话, 要以const auto_ptr&(const + 引用)的方式来传递给其它函数
赋值与安全传递
#include <iostream>
#include <memory>
using namespace std;
template<class T>
ostream& operator<< (ostream& os,const auto_ptr<T>& p){
if(p.get() == NULL)
os<<"NULL";
else
os<<*p;
return os;
}
int main(int argc,char *argv[]){
auto_ptr<int> p(new int(42));
auto_ptr<int> q;
cout<<"after initialization"<<endl;
cout<<"p:"<<p<<endl;
cout<<"q:"<<q<<endl;
q=p;
cout<<"after assign"<<endl;
cout<<"p:"<<p<<endl;
cout<<"q:"<<q<<endl;
*q += 13;
p=q;
cout<<"another assign"<<endl;
cout<<"p:"<<p<<endl;
cout<<"q:"<<q<<endl;
return 0;
}
非安全传递
以下与上面的例子差不多也只是显示auto_ptr所指对象的值, 以非const 引用的方向传递, 结果在函数调用中就把资源给释放了
#include <iostream>
#include <memory>
using namespace std;
template<class T>
ostream& operator<< (ostream& os,auto_ptr<T> p){
if(p.get() == NULL)
os<<"NULL";
else
os<<*p;
return os;
}
int main(int argc,char *argv[]){
auto_ptr<int> p(new int(42));
cout<<p<<endl;
if(p.get() == NULL)
cout<<"after call function, p = NULL"<<endl;
return 0;
}
shared_ptr
#include <iostream>
#include <tr1/memory>
using namespace std;
class base{
public:
base(){cout<<"base init"<<endl;}
virtual ~base(){cout<<"base over"<<endl;}
virtual void show(){cout<<"from base"<<endl;}
};
class derived: public base{
public:
derived(){cout<<"derived init"<<endl;}
~derived(){cout<<"derived over"<<endl;}
void show(){cout<<"from derived"<<endl;}
};
int main () {
tr1::shared_ptr<derived> ptr(new derived());
tr1::shared_ptr<derived> ptr2(ptr);
cout<<ptr.get()<<endl;
cout<<ptr2.get()<<endl;
class derived *dptr=ptr.get();
dptr->show();
return 0;
}
auto_ptr与shared_ptr的更多相关文章
- 初次窥见智能指针auto_ptr和shared_ptr
#include <memory>//shared_ptr要用的头文件 using namespace std; class A //测试auto_ptr和shared_ptr的delet ...
- stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结
stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...
- auto_ptr与shared_ptr ZZ
http://blog.csdn.net/rogeryi/article/details/1442700 Part(1) 这篇文章试图说明如何使用auto_ptr和shared_ptr,从而使得动态分 ...
- auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- auto_ptr和shared_ptr
<Effective C++>在资源管理一节提到了智能指针,智能指针中最著名的当属auto_ptr和shared_ptr.本文主要研究两者的实现. auto_ptr的实现: templat ...
- C++ 之 auto_ptr and shared_ptr
1.auto_ptr 这个所谓的只能指针有点鸡肋! 没有引用计数,而且还有一个所有权转移的情况! 当所有权转移后,以前的auto_ptr将会成为null 2.shared_ptr 增加了引用计数,没 ...
- C++ 智能指针 auto_ptr 和 shared_ptr
首先,如果你不知道什么是智能指针,请先移步:C++智能指针简单剖析 1.auto_ptr #ifndef AUTO_PTR_H #define AUTO_PTR_H template<typen ...
- C++智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr
手写代码是理解C++的最好办法,以几个例子说明C++四个智能指针的用法,转载请注明出处. 一.auto_ptr auto_ptr这是C++98标准下的智能指针,现在常常已经被C++标准的其他智能指针取 ...
- 关于std:auto_ptr std:shared_ptr std:unique_ptr
很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生成健壮的代码.本文阐述了如何正确 ...
- <memory>(包括了auto_ptr,shared_ptr等各种指针)
Memory elements This header defines general utilities to manage dynamic memory: Allocators allocator ...
随机推荐
- Nginx 编译安装工程优化
1.减小 nginx 编译后的文件大小 在编译 nginx 时,默认以 debug 模式进行,在 debug 模式下会插入很多跟踪和 assert 之类的信息. 在 nginx 源码文件解压后,找到源 ...
- Win10遇到蓝屏错误CRITICAL_STRUCTURE_CORRUPTION如何解决
很多使用win10系统的用户,都曾经遇到过蓝屏故障.比如,最近有位win10用户在使用电脑时,就发现电脑突然出现了蓝屏,且提示错误CRITICAL_STRUCTURE_CORRUPTION,这是怎么回 ...
- EffectiveC++01-03
导读 作者Scott Meyers在如何有效运用C++方面给出了55个具体的做法,大致分为两类: 一般性的设计策略,集中于"如何在不同的做法中选择一种完成任务" 选择inherit ...
- php内置函数分析之array_diff_assoc()
static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_type) /* {{{ */ { uint ...
- Django【第15篇】:Django之Form组件归类
Form组件归类 一.Form类 创建Form类时,主要涉及到 [字段] 和 [插件],字段用于对用户请求数据的验证,插件用于自动生成HTML; 1.Django内置字段如下: 1 Field 2 r ...
- apache-2.4.x 编译安装方法
apache-2.4.x 编译安装方法 作者:朱 茂海 /分类:Apache 字号:L M S apache-.2与新出的apache-.4安装不同的地方在于,.4版的已经不自带apr库,所以在安装a ...
- win10如何设置软件开机启动
想要实现应用程序在所有的用户登录系统后都能自动启动,就把该应用程序的快捷方式放到“系统启动文件夹”里C:\ProgramData\Microsoft\Windows\Start Menu\Progra ...
- POJ 1743 Musical Theme ( 后缀数组 && 最长不重叠相似子串 )
题意 : 给 n 个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 分析 : 根据题目对于 “ 相似 ” 串的定义,我们可以将原 ...
- #1113-JSP 语法
JSP 语法 JSP开发中的基础语法. 脚本程序 脚本程序可以包含任意量的Java语句.变量.方法或表达式,只要它们在脚本语言中是有效的. 脚本程序的语法格式: <% 代码片段 %> 或者 ...
- 11 November
Weakness 求数列区间 \(\{a_n\}\) 中满足 \(i < j < k, a_i > a_j > a_k\) 的 \((i, j, k)\) 对的数目. 设对 \ ...