智能指针auto_prt的使用(c++学习笔记)

c++中如果要申请资源一般用到new,最后释放资源delete,如果我们在delete之前就退出了函数呢,看下面的代码:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. //#include <memory>  
  3. using namespace std;  
  4.   
  5. void fun()  
  6. {  
  7.     int *ptr = new int;  
  8.       
  9.     if(ptr == 0)  
  10.     {  
  11.         delete ptr;  
  12.         return;  
  13.     }  
  14.   
  15.     cout << "hello prt" << endl;  
  16.   
  17.     delete ptr;  
  18. }  
  19.   
  20. int main()  
  21. {  
  22.     fun();  
  23.     return 0;  
  24. }  

 

这样我们就要多写一个delete,如果if有很多分支或者别情况呢。甚至有时候我们忘了delete呢,他将导致内存泄露。如果使用智能指针auto_ptr就不会出现这种情况了。
下面我们来看一个例子:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <memory>//auto_ptr 需要用到的头文件  
  3. using namespace std;  
  4.   
  5. void fun(auto_ptr<int> ptr)  
  6. {  
  7.     if (ptr.get() == NULL)  
  8.     {  
  9.         cout << "NULL" << endl;  
  10.     }  
  11.     else  
  12.     {  
  13.         cout << *ptr << endl;  
  14.     }  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     auto_ptr<int> ptr(new int);  
  20.     //auto_ptr<int> ptr = new int(11);  //ERROR  注意智能指针不能使用这种赋值方式  
  21.     *ptr = 44;  
  22.     fun(ptr);  
  23.   
  24.     //cout << *ptr << endl;  
  25.     if (ptr.get() == NULL)  
  26.     {  
  27.         cout << "delete" << endl;  
  28.     }  
  29.       
  30.     return 0;  
  31. }  

编译后结果为 44    delete
很明显我们可以看到ptr被释放掉了。
注意,智能指针有拥有权转移的性质,ptr1给ptr2赋值后,ptr1就被delete掉了就不能再取他的值了。代码如下:

[c-sharp]  view plain  copy
  1. #include <iostream>  
  2. #include <memory>  
  3. using namespace std;  
  4.   
  5. ostream& operator<< (ostream& otm , auto_ptr<int>& ptr)  
  6. {  
  7.     if(ptr.get() == NULL)  
  8.     {  
  9.         otm << "NULL" << endl;  
  10.     }  
  11.     else  
  12.     {  
  13.         otm << *ptr << endl;  
  14.     }  
  15.   
  16.     return otm;  
  17. }  
  18.   
  19. int main()  
  20. {  
  21.     auto_ptr<int> ptr1(new int(22));  
  22.     auto_ptr<int> ptr2;  
  23.   
  24.     cout << ptr1 << endl;  
  25.     cout << ptr2 << endl;  
  26.   
  27.     ptr2 = ptr1;  
  28.     cout << ptr1 << endl;  
  29.     cout << ptr2 << endl;  
  30.       
  31.     return 0;  
  32. }  

编译后结果为22  NULL  NULL  22
可以看到执行ptr2 = ptr1后ptr1指向了NULL。
如果我们不希望看到拥有权的改变那就要在声明是加const。拥有权不可以改变但你可以取他的值来赋值,例如:*ptr1 = * prt2。

 

 

下面附一份《c++标准程序库》中提供的auto_ptr的实现代码,看了下,跟《c++程序设计语言》中的差不多,但跟具体,据说比标准库的有一些改进。

[cpp]  view plain  copy
  1. /* The following code example is taken from the book 
  2.  * "The C++ Standard Library - A Tutorial and Reference" 
  3.  * by Nicolai M. Josuttis, Addison-Wesley, 1999 
  4.  * 
  5.  * (C) Copyright Nicolai M. Josuttis 1999. 
  6.  * Permission to copy, use, modify, sell and distribute this software 
  7.  * is granted provided this copyright notice appears in all copies. 
  8.  * This software is provided "as is" without express or implied 
  9.  * warranty, and with no claim as to its suitability for any purpose. 
  10.  */  
  11. /* class auto_ptr 
  12.  * - improved standard conforming implementation 
  13.  */  
  14. namespace std {  
  15.     // auxiliary type to enable copies and assignments (now global)  
  16.     template<class Y>  
  17.     struct auto_ptr_ref {  
  18.         Y* yp;  
  19.         auto_ptr_ref (Y* rhs)  
  20.          : yp(rhs) {  
  21.         }  
  22.     };  
  23.   
  24.     template<class T>  
  25.     class auto_ptr {  
  26.       private:  
  27.         T* ap;    // refers to the actual owned object (if any)  
  28.       public:  
  29.         typedef T element_type;  
  30.   
  31.         // constructor  
  32.         explicit auto_ptr (T* ptr = 0) throw()  
  33.          : ap(ptr) {  
  34.         }  
  35.   
  36.         // copy constructors (with implicit conversion)  
  37.         // - note: nonconstant parameter  
  38.         auto_ptr (auto_ptr& rhs) throw()  
  39.          : ap(rhs.release()) {  
  40.         }  
  41.         template<class Y>  
  42.         auto_ptr (auto_ptr<Y>& rhs) throw()  
  43.          : ap(rhs.release()) {  
  44.         }  
  45.           
  46.         // assignments (with implicit conversion)  
  47.         // - note: nonconstant parameter  
  48.         auto_ptr& operator= (auto_ptr& rhs) throw() {  
  49.             reset(rhs.release());  
  50.             return *this;  
  51.         }  
  52.         template<class Y>  
  53.         auto_ptr& operator= (auto_ptr<Y>& rhs) throw() {  
  54.             reset(rhs.release());  
  55.             return *this;  
  56.         }  
  57.           
  58.         // destructor  
  59.         ~auto_ptr() throw() {  
  60.             delete ap;  
  61.         }  
  62.   
  63.         // value access  
  64.         T* get() const throw() {  
  65.             return ap;  
  66.         }  
  67.         T& operator*() const throw() {  
  68.             return *ap;  
  69.         }  
  70.         T* operator->() const throw() {  
  71.             return ap;  
  72.         }  
  73.   
  74.         // release ownership  
  75.         T* release() throw() {  
  76.             T* tmp(ap);  
  77.             ap = 0;  
  78.             return tmp;  
  79.         }  
  80.   
  81.         // reset value  
  82.         void reset (T* ptr=0) throw() {  
  83.             if (ap != ptr) {  
  84.                 delete ap;  
  85.                 ap = ptr;  
  86.             }  
  87.         }  
  88.   
  89.         /* special conversions with auxiliary type to enable copies and assignments 
  90.          */  
  91.         auto_ptr(auto_ptr_ref<T> rhs) throw()  
  92.          : ap(rhs.yp) {  
  93.         }  
  94.         auto_ptr& operator= (auto_ptr_ref<T> rhs) throw() {  // new  
  95.              reset(rhs.yp);  
  96.              return *this;  
  97.         }  
  98.         template<class Y>  
  99.         operator auto_ptr_ref<Y>() throw() {  
  100.             return auto_ptr_ref<Y>(release());  
  101.         }  
  102.         template<class Y>  
  103.         operator auto_ptr<Y>() throw() {  
  104.             return auto_ptr<Y>(release());  
  105.         }  
  106.     };  
  107. }  



FROM: http://blog.csdn.net/z0203153008/article/details/4652046






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值