template<class T>
  class auto_ptr {
  public:
   explicit auto_ptr(T *p = ); // Item M5 有“explicitfor”// 的描述
  
template<class U> // 拷贝构造函数成员模板
   auto_ptr(auto_ptr<U>& rhs); // (见Item M28):
   // 用另一个类型兼容的
   // auto_ptr对象
   // 初始化一个新的auto_ptr对象    ~auto_ptr();
   template<class U> // 赋值操作成员模板
   auto_ptr<T>& // (见Item M28):
   operator=(auto_ptr<U>& rhs); // 用另一个类型兼容的    // auto_ptr对象给它赋值
   T& operator*() const; // 见Item M28
   T* operator->() const; // 见Item M28
   T* get() const; // 返回包容指针的
   // 当前值
   T* release(); // 放弃包容指针的
   // 所有权,
   // 并返回其当前值
   void reset(T *p = ); // 删除包容指针,
   // 获得指针p的所有权
  private:
   T *pointee;   template<class U> // 让所有的auto_ptr类
  friend class auto_ptr<U>; // 成为友元   };
     //构造函数
  template<class T>
  inline auto_ptr<T>::auto_ptr(T *p)
  : pointee(p)
  {}
//拷贝构造函数
  template<class T>
   inline auto_ptr<T>::auto_ptr(auto_ptr<U>& rhs)
   : pointee(rhs.release())
   {}   template<class T>
  inline auto_ptr<T>::~auto_ptr()
  { delete pointee; }    template<class T>
   template<class U>
   inline auto_ptr<T>& auto_ptr<T>::operator=(auto_ptr<U>& rhs)
   {
   if (this != &rhs) reset(rhs.release());
   return *this;
   }   template<class T>
  inline T& auto_ptr<T>::operator*() const
  { return *pointee; }   template<class T>
  inline T* auto_ptr<T>::operator->() const
  { return pointee; }   template<class T>
  inline T* auto_ptr<T>::get() const
  { return pointee; }   template<class T>
  inline T* auto_ptr<T>::release()
  {
   T *oldPointee = pointee;
   pointee = ;
   return oldPointee;
  }   template<class T>
  inline void auto_ptr<T>::reset(T *p)
  {
   if (pointee != p) {
   delete pointee;
   pointee = p;
   }
  }

没看过stl源码剖析,感觉这个智能指针只是在自己要管理的内存资源上封装成类,这样利用对象解析时自动调用析构函数的思想,来保证delete的执行。

//摘自网上http://dbchu.com/2013/05/11/966

STL中智能指针 auto_ptr(C++98)所做的事情,就是动态分配对象以及当对象不再需要时自动执行清理。

但是auto_ptr有缺陷,在使用时要注意避免。

不要将auto_ptr对象作为STL容器的元素。C++标准明确禁止这样做,否则可能会碰到不可预见的结果。

1、auto_ptr不能共享所有权。
2、auto_ptr不能指向数组
3、auto_ptr不能作为容器的成员。
4、不能通过赋值操作来初始化auto_ptr
std::auto_ptr<int> p(new int(42)); //OK
std::auto_ptr<int> p = new int(42); //ERROR
这是因为auto_ptr 的构造函数被定义为了explicit
5、不要把auto_ptr放入容器

C++ 智能指针auto_ptr的更多相关文章

  1. C++智能指针(auto_ptr)详解

    智能指针(auto_ptr) 这个名字听起来很酷是不是?其实auto_ptr 只是C++标准库提供的一个类模板,它与传统的new/delete控制内存相比有一定优势,但也有其局限.本文总结的8个问题足 ...

  2. 自己动手实现智能指针auto_ptr

    面试的时候,我们经常会被问到如何自己动手实现智能指针auto_ptr.今天我就一边参考STL库中的源代码,一边将auto_ptr的实现敲一遍. auto_ptr归根到底是一个模版类,那么这个类要实现哪 ...

  3. 关于智能指针auto_ptr

    智能指针auto_ptr和shared_ptr也是面试中经常被问到的一个 感觉看auto_ptr的源码反而更加容易理解一些,因为源码的代码量并不大,而且比较容易理解. 本篇主要介绍auto_ptr 其 ...

  4. C++中的智能指针(auto_ptr)

    实际上auto_ptr 仅仅是C++标准库提供的一个类模板,它与传统的new/delete控制内存相比有一定优势.使用它不必每次都手动调用delete去释放内存.当然有利也有弊,也不是全然完美的. 本 ...

  5. 【C++】智能指针auto_ptr简单的实现

    //[C++]智能指针auto_ptr简单的实现 #include <iostream> using namespace std; template <class _Ty> c ...

  6. 智能指针auto_ptr & shared_ptr

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

  7. C++智能指针 auto_ptr

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

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

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

  9. 【C++智能指针 auto_ptr】

    <More Effective C++>ITEM M9他提到auto_ptr.说是当异常产生的时候.怎么释放为对象分配的堆内存,避免反复编写内存释放语句. PS:这里书里面提到函数退出问题 ...

随机推荐

  1. Spark计算模型-RDD介绍

    在Spark集群背后,有一个非常重要的分布式数据架构,即弹性分布式数据集(Resilient Distributed DataSet,RDD),它是逻辑集中的实体,在集群中的多台集群上进行数据分区.通 ...

  2. cocos2dx lua binding ,cocos2dx 绑定lua测试

    前面2篇分别简单介绍 手动bind C++ 类和lua:http://blog.csdn.net/chenee543216/article/details/12074771 使用tolua++简化工作 ...

  3. C++ —— 编译程序

    目录: 0.GCC online documentation 1.gcc编译器 常用命令 2.VC编译器  常用参数说明 3.C预处理器命令说明 4.debug 和 release 的区别 0.GCC ...

  4. 关于Hibemate

    1.Hibernate定位 HIbernate是一款实现了ORM思想的框架 JDO TOpLink 2.HIbernate初次解释 Hibernate:冬眠,蛰伏 和持久化有关系 将内存中data持久 ...

  5. Jenkins+maven+git+sonar 系统持续集成&amp;代码单測管理

    Jenkins+maven+git+sonar 系统持续集成&代码单測管理 Jenkins的安装 Jenkins是基于Java开发的一种持续集成工具,用于监控持续反复的工作.功能包含: 1.持 ...

  6. Android ImageView 点击更换头像

    首先搭建布局 主界面布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  7. Php面向对象 – 单例模式

    Php面向对象 – 单例模式 保证类仅仅有一个实例 1.    怎样能够解决一个类能够被无限地实例化? New,就能实例化一次,怎么去限制,用户不能无限次地new? 将构造方法私有化.全部外部的new ...

  8. LVS+Keepalived实现MySQL从库读操作负载均衡

    http://www.osyunwei.com/archives/7464.html (学习运维知识好站) 说明: 操作系统:CentOS 5.X 64位 MySQL主服务器:192.168.21.1 ...

  9. Android消息机制(2)

    在Android 中,线程内部或者线程之间进行信息交互时经常会使用消息,这些基础的东西如果我们熟悉其内部的原理,将会使我们容易.更好地架构系统,避免一些低级的错误. 下面我们分析下程序的运行过程: 1 ...

  10. Linux crontab 命令详解(含配置文件路径)

    编辑/etc/crontab 文件配置cron cron 服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用cron服 ...