STL+auto_ptr智能指针简单分析

STL auto_ptr智能指针简单分析 

2011年10月8日 代码疯子

闲着没事,整理一下对auto_ptr的理解。去年年底的时候准备认认真真的看看STL源代码,可是时间不是很充足,自己也不是很坚定,于是就乱七八糟的看了看。现在虽然琐事繁多,但时间还是有的,所以再整理下。

auto_ptr是STL里面的智能指针(Smart Pointer),一个很好的优点就是指针所有权自动转移和指针自动删除技术。对于异常和经常忘记delete的情况来说很实用。下面就是从SGI官方网站下载的STL auto_ptr实现源码(加上了我的注释):

[cpp]  view plain copy
  1. /* 
  2. * Copyright (c) 1997-1999 
  3. * Silicon Graphics Computer Systems, Inc. 
  4. * 
  5. * Permission to use, copy, modify, distribute and sell this software 
  6. * and its documentation for any purpose is hereby granted without fee, 
  7. * provided that the above copyright notice appear in all copies and 
  8. * that both that copyright notice and this permission notice appear 
  9. * in supporting documentation.  Silicon Graphics makes no 
  10. * representations about the suitability of this software for any 
  11. * purpose.  It is provided "as is" without express or implied warranty. 
  12. * 
  13. */  
  14.    
  15. #ifndef __SGI_STL_MEMORY  
  16. #define __SGI_STL_MEMORY  
  17.    
  18. #include <stl_algobase.h>  
  19. #include <stl_alloc.h>  
  20. #include <stl_construct.h>  
  21. #include <stl_tempbuf.h>  
  22. #include <stl_uninitialized.h>  
  23. #include <stl_raw_storage_iter.h>  
  24.    
  25.    
  26. __STL_BEGIN_NAMESPACE  
  27. // 如果定义了auto_ptr转换以及支持成员函数模板  
  28. #if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \  
  29.     defined(__STL_MEMBER_TEMPLATES)  
  30. // 定义auto_ptr_ref template结构体①  
  31. template<class _Tp1> struct auto_ptr_ref {  
  32.     _Tp1* _M_ptr;  
  33.     auto_ptr_ref(_Tp1* __p) : _M_ptr(__p) {}  
  34. };  
  35.    
  36. #endif  
  37.    
  38. template <class _Tp>  
  39. class auto_ptr {  
  40. private:  
  41.     _Tp* _M_ptr;  
  42.    
  43. public:  
  44.     typedef _Tp element_type;  
  45.     // explicit修饰构造函数,防止从原始指针隐式转换  
  46.     explicit auto_ptr(_Tp* __p = 0) __STL_NOTHROW : _M_ptr(__p) {}  
  47.     // Copy构造函数,注意这里是直接引用传参(非const),同时转移指针所有权  
  48.     auto_ptr(auto_ptr& __a) __STL_NOTHROW : _M_ptr(__a.release()) {}  
  49.    
  50. // 如果允许定义成员函数模板(Member Function Templates)②  
  51. #ifdef __STL_MEMBER_TEMPLATES  
  52.     // 如果可以从_Tp1*转换为_Tp*,则可以从auto_ptr<_Tp1>构造auto_ptr<_Tp>  
  53.     // 同时转移指针所有权  
  54.     template <class _Tp1>  
  55.     auto_ptr(auto_ptr<_Tp1>& __a) __STL_NOTHROW  
  56.         : _M_ptr(__a.release()) {}  
  57. #endif /* __STL_MEMBER_TEMPLATES */  
  58.    
  59.     // 赋值操作符,同样是非const引用传参,有证同测试③  
  60.     auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW {  
  61.         // 如果是自我赋值,就直接返回  
  62.         if (&__a != this) {  
  63.             delete _M_ptr;  
  64.             _M_ptr = __a.release();  
  65.         }  
  66.         return *this;  
  67.     }  
  68.    
  69. #ifdef __STL_MEMBER_TEMPLATES  
  70.     // 赋值操作符的Member Function Templates  
  71.     template <class _Tp1>  
  72.     auto_ptr& operator=(auto_ptr<_Tp1>& __a) __STL_NOTHROW {  
  73.         if (__a.get() != this->get()) {  
  74.             delete _M_ptr;  
  75.             _M_ptr = __a.release();  
  76.         }  
  77.         return *this;  
  78.     }  
  79. #endif /* __STL_MEMBER_TEMPLATES */  
  80.    
  81.     // Note: The C++ standard says there is supposed to be an empty throw  
  82.     // specification here, but omitting it is standard conforming.  Its   
  83.     // presence can be detected only if _Tp::~_Tp() throws, but (17.4.3.6/2)  
  84.     // this is prohibited.  
  85.     // auto_ptr的析构函数  
  86.     ~auto_ptr() { delete _M_ptr; }  
  87.     // operator*定义,返回值  
  88.     _Tp& operator*() const __STL_NOTHROW {  
  89.         return *_M_ptr;  
  90.     }  
  91.     // operator->定义,返回指针  
  92.     _Tp* operator->() const __STL_NOTHROW {  
  93.         return _M_ptr;  
  94.     }  
  95.     // const成员函数get定义,返回指针  
  96.     _Tp* get() const __STL_NOTHROW {  
  97.         return _M_ptr;  
  98.     }  
  99.     // release函数定义,释放指针  
  100.     _Tp* release() __STL_NOTHROW {  
  101.         _Tp* __tmp = _M_ptr;  
  102.         _M_ptr = 0;  
  103.         return __tmp;  
  104.     }  
  105.     // reset函数定义,重置指针  
  106.     void reset(_Tp* __p = 0) __STL_NOTHROW {  
  107.         if (__p != _M_ptr) {  
  108.             delete _M_ptr;  
  109.             _M_ptr = __p;  
  110.         }  
  111.     }  
  112.    
  113.     // According to the C++ standard, these conversions are required.  Most  
  114.     // present-day compilers, however, do not enforce that requirement---and,   
  115.     // in fact, most present-day compilers do not support the language   
  116.     // features that these conversions rely on.  
  117.    
  118. #if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \  
  119.     defined(__STL_MEMBER_TEMPLATES)  
  120.    
  121. public:  
  122.     // 从auto_ptr_ref<_Tp>构造auto_ptr<_Tp>  
  123.     auto_ptr(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW  
  124.         : _M_ptr(__ref._M_ptr) {}  
  125.     // 从auto_ptr_ref<_Tp>对auto_ptr<_Tp>进行赋值操作。  
  126.     // 注意这里是普通传参,没有引用④  
  127.     auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW {  
  128.         if (__ref._M_ptr != this->get()) {  
  129.             delete _M_ptr;  
  130.             _M_ptr = __ref._M_ptr;  
  131.         }  
  132.         return *this;  
  133.     }  
  134.     // 成员函数模板(Member Function Templates)②  
  135.     // 如果可以从_Tp*转换为_Tp1*,则可以从auto_ptr<_Tp>转换为auto_ptr_ref<_Tp1>  
  136.     template <class _Tp1> operator auto_ptr_ref<_Tp1>() __STL_NOTHROW   
  137.     { return auto_ptr_ref<_Tp1>(this->release()); }  
  138.     // 成员函数模板(Member Function Templates)②  
  139.     // 如果可以从_Tp*转换为_Tp1*,则可以从auto_ptr<_Tp>转换为auto_ptr<_Tp1>  
  140.     template <class _Tp1> operator auto_ptr<_Tp1>() __STL_NOTHROW  
  141.     { return auto_ptr<_Tp1>(this->release()); }  
  142.    
  143. #endif /* auto ptr conversions && member templates */  
  144. };  
  145.    
  146. __STL_END_NAMESPACE  
  147.    
  148. #endif /* __SGI_STL_MEMORY */  
  149.    
  150. // Local Variables:  
  151. // mode:C++  
  152. // End:  

注解:

①auto_ptr_ref结构体
我们看到,auto_ptr源代码中的Copy构造函数的参数是普通的引用传参(不是const引用,也不是普通的传值),这是为了方便指针拥有权的转移(如果是const引用,那么拥有权无法转移;如果是普通的传值,oh my god,整个世界都彻底混乱了)。那如果以一个临时对象(也就是所谓的右值)进行拷贝构造,那样就无法通过编译了(普通指针或引用不能指向const对象,即不能指向右值)。幸好有auto_ptr_ref的存在,可以从auto_ptr_ref临时对象构造或者赋值为auto_ptr对象:

[cpp]  view plain copy
  1. public:  
  2.     // 从auto_ptr_ref<_Tp>构造auto_ptr<_Tp>  
  3.     auto_ptr(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW  
  4.         : _M_ptr(__ref._M_ptr) {}  
  5.     // 从auto_ptr_ref<_Tp>对auto_ptr<_Tp>进行赋值操作。  
  6.     // 注意这里是普通传参,没有引用④  
  7.     auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW {  
  8.         if (__ref._M_ptr != this->get()) {  
  9.             delete _M_ptr;  
  10.             _M_ptr = __ref._M_ptr;  
  11.         }  
  12.         return *this;  
  13.     }  

而auto_ptr对象也可以隐式的转化为auto_ptr_ref类型的对象:

[cpp]  view plain copy
  1. template <class _Tp1> operator auto_ptr_ref<_Tp1>() __STL_NOTHROW   
  2.     { return auto_ptr_ref<_Tp1>(this->release()); }  

于是乎,就完美的完成了auto_ptr从右值到左值的转换工作。也可以看这里: 为什么需要auto_ptr_ref

成员函数模板(Member Function Templates)
③证同测试,见《Effective C++》条款11:在operator= 中处理“自我赋值” (Item 11. handle assignment to self in operator=)
④见①

原创文章,转载请注明:
本文出自程序人生 >> STL auto_ptr智能指针简单分析
作者:代码疯子

您可能对下面的文章也感兴趣:
  1. Member Function Templates(成员函数模板) (12.8)
  2. delete Operator in C++ (9.3)
  3. 前置后置自增自减操作符重载 (8.6)
  4. C++ const成员函数 (8)
  5. delete与delete[] (7.8)

转自:http://blog.csdn.net/msbsod/article/details/6867648
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值