注: 从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的更多相关文章

  1. 初次窥见智能指针auto_ptr和shared_ptr

    #include <memory>//shared_ptr要用的头文件 using namespace std; class A //测试auto_ptr和shared_ptr的delet ...

  2. stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结

    stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...

  3. auto_ptr与shared_ptr ZZ

    http://blog.csdn.net/rogeryi/article/details/1442700 Part(1) 这篇文章试图说明如何使用auto_ptr和shared_ptr,从而使得动态分 ...

  4. auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  5. auto_ptr和shared_ptr

    <Effective C++>在资源管理一节提到了智能指针,智能指针中最著名的当属auto_ptr和shared_ptr.本文主要研究两者的实现. auto_ptr的实现: templat ...

  6. C++ 之 auto_ptr and shared_ptr

    1.auto_ptr 这个所谓的只能指针有点鸡肋!  没有引用计数,而且还有一个所有权转移的情况! 当所有权转移后,以前的auto_ptr将会成为null 2.shared_ptr 增加了引用计数,没 ...

  7. C++ 智能指针 auto_ptr 和 shared_ptr

    首先,如果你不知道什么是智能指针,请先移步:C++智能指针简单剖析 1.auto_ptr #ifndef AUTO_PTR_H #define AUTO_PTR_H template<typen ...

  8. C++智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr

    手写代码是理解C++的最好办法,以几个例子说明C++四个智能指针的用法,转载请注明出处. 一.auto_ptr auto_ptr这是C++98标准下的智能指针,现在常常已经被C++标准的其他智能指针取 ...

  9. 关于std:auto_ptr std:shared_ptr std:unique_ptr

    很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生成健壮的代码.本文阐述了如何正确 ...

  10. <memory>(包括了auto_ptr,shared_ptr等各种指针)

    Memory elements This header defines general utilities to manage dynamic memory: Allocators allocator ...

随机推荐

  1. Python变量类型及变量

    python是解释性语言 什么是解释性语言 就相当于你去饭店,你点了10道菜,他做好1道给你上1道.解释一行,执行一行.速度上不如编译性语言快. 什么是编译性语言 就相当于去饭店吃饭,你点了10道菜, ...

  2. jvm slot复用

    如果当前字节码PC计数器的值已经超出了某个变量的作用域,那这个变量对应的Slot就可以交给其他变量使用. 字节码PC计数器就是程序计数器,程序计数器记录当前线程所执行的字节码的偏移地址.如果这个值超出 ...

  3. pycharm设置SDK

    1.一看到这个提示,就知道Pycharm中尚未配置Python解释器,此时不用慌,并不是Pycharm没有安装成功,而是因为有个配置尚未完成,只需要配置好Python解释器之后,一切都会正常.其实Py ...

  4. spring security基本知识(三) 过滤详细说明

    在我们前面的文章Spring Security 初识(一)中,我们看到了一个最简单的 Spring Security 配置,会要求所有的请求都要经过认证.但是,这并不是我们想要的,我们通常想自定义应用 ...

  5. html2canvas 把h5网页保存为图片 区域保存

    html2canvas 把h5网页保存为图片 想把一个网页得某些元素,绘制成图片保存,有些数据是接口动态加载的,所以不能UI给到图片,需要我们把api的数据也绘制到图片上 html2canvas这个插 ...

  6. hdu 4609: 3-idiots (FFT)

    题目链接 题意:从N个数中,选出三个两两不同的数,求这三个数能够作为一个三角形的三边长的概率. 题解:用一个数组num[]记录大小为 i 的数出现的次数,通过 num[] 卷 num[] 得到 num ...

  7. Java面试之基础篇(1)

    1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致. 2.Java有 ...

  8. A1009

    两个多项式的乘积 两个数组,一个放多项式1,一个放结果 注意:arr2[j+exp]+=arr1[j]*coe; 因为有指数相加相同的情况下需要合并系数 #include<cstdio> ...

  9. appium环境666

    一.安装JDK,配置JDK环境    appium下载地址: https://github.com/appium/appium-desktop/releases/ 百度搜索下载就行,这里分享一个下载链 ...

  10. 前端面试题常考&必考之--清除浮动的方法

    浮动 问题:子元素设置了float后,脱离父元素,导致父元素无法撑开?(也就是子元素的高度没有过渡到父元素) 例子: 检查元素的效果: (三种)常用的解决办法: 1>额外标签法,添加一个空的di ...