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. casperjs配合phantomjs实现自动登录百度,模拟点击等等操作 - 怕虎在线www.ipahoo.com图文教程 - 怕虎在线

    微信支付取消2万元保证金门槛,这是全民电商来袭!-观点-虎嗅网 微信支付取消2万元保证金门槛,这是全民电商来袭! casperjs配合phantomjs实现自动登录百度,模拟点击等等操作 - 怕虎在线 ...

  2. 标准的数据获取 -ios

    #define kBgQueue dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) #define kLatestKivaL ...

  3. ListView列表拖拽排序

    ListView列表拖拽排序能够參考Android源代码下的Music播放列表,他是能够拖拽的,源代码在[packages/apps/Music下的TouchInterceptor.java下]. 首 ...

  4. 了解SVG

    页的节点类型,我们将说明怎样通过Illustrator高速的把SVG文档加入到网页中.我们还会讲讲D3.js,一个强大的.SVG控制的JavaScript库. "SVG并不仅仅用于像素处理. ...

  5. android 程序防止被360或者系统给kill掉

    关于如果和防止android 程序防止被360kill掉之后重启的问题,肯定大家也搜索了好多方法,都不好使,对不对,什么增高权限了,什么进程优先级了,这些东西都不是我们可控的,所以有没有一些非常保险的 ...

  6. android PreferenceScreen使用笔记

    preference.xml <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen ...

  7. unity3d Find()使用

    1. Hierarchy 创建对象如两个cube时,未修改名称,名称都为cube时. js添加至Camera: private var cubeObj : GameObject; //private ...

  8. how to get file from classpath using jboss7.x.1 --reference

    question: I want to convert smooks xml-java, so that i need to load source file from mobeeadmin.war/ ...

  9. struts2,hibernate,spring整合笔记(2)

    上一话struts2,hibernate,spring整合笔记(1) 接下来继续 配置完struts之后就要开始hibernate的配置 hibernate的环境并不依赖web开发环境,在我第一次配置 ...

  10. Chapter 4. Using the Gradle Command-Line 使用gradle命令行

    This chapter introduces the basics of the Gradle command-line. You run a build using the gradle comm ...