注:C++11 已不推荐使用,应使用scoped_ptr/shared_ptr. 部分原因就是如下的注意事项。

转自:http://patmusing.blog.163.com/blog/static/13583496020101824541270/

a. auto_ptr定义于头文件memory中;

b. auto_ptr只能用来管理单个动态创建的对象,而不能管理动态创建的数组;

c. 和其他copy和assign不同,auto_ptr的copy和assign会改变右边的操作数,assignment符号的两边的auto_ptr均为左值;There is a crucially important difference between how auto_ptr and built-in pointers treat copy and assignment. When we copy an auto_ptr or assign its value to another auto_ptr, ownership of the underlying object is transferred from the original to the copy. The original auto_ptr is reset to an unbound state;

d. auto_ptr不能作为容器中的元素;

auto_ptr的copy和assign具有析构行为,这就是auto_ptr不能作为容器元素的原因,因为标准库中的容器有对元素的要求:经

过copy或者assign后的两个对象,必须相等;

e. 在判断一个auto_ptr是否被绑定的时候,不能直接使用auto_ptr对象:

auto_ptr<Student> stu1(new Student);

if(stu1)

{

cout << "stu1 is bound" << endl;

}

else

{

cout << "stu1 is unbound" << endl;

}

这样做将会导致compile error,应该改为:

auto_ptr<Student> stu1(new Student);

if(stu1.get()) // get()获取的是underlying对象的指针,如果被绑定则非零,如果没有被绑定则为0

{

cout << "stu1 is bound" << endl;

}

else

{

cout << "stu1 is unbound" << endl;

}

f. auto_ptr的构造函数是explicit的,消除了隐式的类型转换(在这里即,从指针类型到auto_ptr类型的转换),因此不能直接将一个

指针赋给一个auto_ptr对象。如下面这样的代码:

auto_ptr<Student> stu5 = new Student;

stu5->printStudentInfo();

在编译的时候不会有问题,但会出现严重的runtime error。正确的做法应该是:

auto_ptr<Student> stu5(new Student);

stu5->printStudentInfo();

g. 不同用两个auto_ptr绑定到同一个对象。

// stu6和stu7绑定到了同一个对象,这将会导致该对象被析构两次,将会产生runtime error

auto_ptr<Student> stu6(new Student("Evanligine", "F", 8));

auto_ptr<Student> stu7(stu6.get());

后面一句,应该改为:

auto_ptr<Student> stu7(stu6);

这样stu6就将ownership转交给了stu7,stu6则成为了unbound的auto_ptr。

h. 不能用auto_ptr指向静态资源分配对象。如下面的代码,尽管可以通过编译,但将会产生runtime error:

int ix = 10;

auto_ptr<int> pint1(&ix);

i. auto_ptr的重要操作

auto_ptr<T> ap; 创建一个未绑定的auto_ptr对象ap

auto_ptr<T> ap(p); 创建一个auto_ptr对象ap,它绑定了指针p所指向的对象。该构造函数是explicit的

auto_ptr<T> ap1(ap2); 创建一个auto_ptr对象ap1,它绑定到原来被ap2绑定的对象,ap2则成为未绑定的auto_ptr

ap1 = ap2; ap1删除原来绑定的对象,ap2将ownership移交给ap1,ap2成为未绑定的auto_ptr

*ap 返回ap绑定的对象的引用。可以通过*给被绑定的内在对象赋值。如下面代码:

auto_ptr<int> pint(new int(3));

cout << *pint << endl; // 输出3

*pint = 100;

cout << *pint << endl; // 输出100

ap-> 返回被ap绑定的对象的指针

ap.reset(p) 如果指针p和ap绑定的内存对象的指针不相同,那么ap删除被其绑定的内存对象,改而绑定p所

指向的对象.

ap.release() 返回ap所绑定对象的指针,并使ap成为未绑定对象

ap.get() 返回ap所绑定对象的指针

使用auto_ptr需要注意的事项的更多相关文章

  1. c++ auto_ptr笔记

    1.auto_ptr 不可以使用指针惯用的赋值初始化方式,只能直接初始化. 示例:  char *p = 'A';//error  auto_ptr<char>ptr = new char ...

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

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

  3. c++11 auto_ptr介绍

    在代码里面看到了auto_ptr这个东西,正好以前一哥们曾经问过我这个问题..所以特意去搜了搜帖子,学习学习 http://www.cnblogs.com/gaoxianzhi/p/4451803.h ...

  4. 智能指针auto_ptr & shared_ptr

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

  5. C++ Primer 与“类”有关的注意事项总结

    C++ 与"类"有关的注意事项总结(一) 1. 除了静态 static 数据成员外,数据成员不能在类体中被显式地初始化. 例如 : class First { int memi = ...

  6. C++智能指针 auto_ptr

    C++智能指针 auto_ptr auto_ptr 是一个轻量级的智能指针, 定义于 memory (非memory.h)中, 命名空间为 std. auto_ptr 适合用来管理生命周期比较短或者不 ...

  7. auto_ptr类

    auto_ptr是一个模板类,用于管理动态内存分配. 请看下面的函数: void remodel (string& str) { string * ps = new string(str); ...

  8. shared_ptr 和auto_ptr智能指针

    shared_ptr:计数的智能指针 它是一个包装了new操作符在堆上分配的动态对象,但它实现的是引用计数型的智能指针 ,可以被自由地拷贝和赋值,在任意的地方共享它,当没有代码使用(引用计数为0)它时 ...

  9. C++智能指针--auto_ptr指针

    auto_ptr是C++标准库提供的类模板,头文件<memory>,auto_ptr对象通过初始化指向由new创建的动态内存,它是这块内存的拥有者,一块内存不能同一时候被分给两个拥有者.当 ...

随机推荐

  1. 004 Android XML文件常用五大页面布局方式

    1.线性布局(LinearLayout)最常用 <1>使用线性布局,首先在xml文件中修改布局为LinearLayout 修改完成后,可在Component Tree中看见如下内容: &l ...

  2. linux下安装使用虚拟环境

    一.导语 在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同 ...

  3. panda强化练习2

    In [1]: import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sa ...

  4. vue 浏览器顶部有载入(进度)动画插件vue-progressbar

    1.安装 npm install --save nprogress 2.在main.js中引入 import NProgress from "nprogress" import & ...

  5. jacob自己动生成word文档目录

    任务目的 1自动生成word文档目录. 用例测试操作步骤 在一个word文档的第二页填写占位符: {目录}保存.调用程序读取目标文档,自动根据标题生成目录到{目录}位置. 效果 关键代码 insert ...

  6. MAYA 卸载工具,完美彻底清除干净maya各种残留注册表和文件

    是不是遇到MAYA/CAD/3DSMAX/INVENTOR安装失败?AUTODESK系列软件着实令人头疼,MAYA/CAD/3DSMAX/INVENTOR安装失败之后不能完全卸载!!!(比如maya, ...

  7. [Activator-HelloAkka] Define our Actors

    The Actor is the unit of execution in Akka. Actors are object-oriented in the sense that they encaps ...

  8. bzoj 3576: [Hnoi2014]江南乐

    Description 小A是一个名副其实的狂热的回合制游戏玩家.在获得了许多回合制游戏的世界级奖项之后,小A有一天突然想起了他小时候在江南玩过的一个回合制游戏. 游戏的规则是这样的,首先给定一个数F ...

  9. jQuery easyUI 的combogrid进行模糊匹配

    $(function(){ $('#search_hospitalName').combogrid({ mode : 'remote',//远程连接方式 //fitColumns:true,//自动大 ...

  10. VS 正则表达式替换内容

    很少使用VS的正则替换功能,最近因为需要添加大量的默认值,但是又不想重新类,就想到了这个. 1.替换带有///描述的属性 查找的正则表达式: /// <summary>((.)*((.|\ ...