auto_ptr V.S. unique_ptr

  1. // TestAutoPtr.cpp : Defines the entry point for the console application.  
  2. //  
  3.  
  4. #include "stdafx.h"  
  5. #include <vector>  
  6. #include <memory>  
  7.  
  8. class A  
  9. {  
  10. public:  
  11.     A();  
  12.     ~A();  
  13.  
  14.     void SetA(int a);  
  15.     int GetA() const;  
  16.  
  17. private:  
  18.     int m_a;  
  19. };  
  20.  
  21. A::A()  
  22.     :m_a(0)  
  23. {  
  24.  
  25. }  
  26.  
  27. A::~A()  
  28. {  
  29.  
  30. }  
  31.  
  32. void A::SetA(int a)  
  33. {  
  34.     m_a = a;  
  35. }  
  36.  
  37. int A::GetA() const 
  38. {  
  39.     return m_a;  
  40. }  
  41.  
  42. void Foo(std::auto_ptr<A> apA)  
  43. {  
  44.     int i=3;  
  45. }  
  46.  
  47. void Foo(std::unique_ptr<A> upA)  
  48. {  
  49.     int j=3;  
  50. }  
  51.  
  52. void Foo2(std::auto_ptr<A>& apA)  
  53. {  
  54.     int i=3;  
  55.  
  56.     std::auto_ptr<A> ap(new A());  
  57.     apA = ap;  
  58. }  
  59.  
  60. std::auto_ptr<A> Foo()  
  61. {  
  62.     std::auto_ptr<A> ap(new A());  
  63.     return ap;  
  64. }  
  65.  
  66. int _tmain(int argc, _TCHAR* argv[])  
  67. {  
  68.     //std::auto_ptr<A> apA(new A());  
  69.     //std::unique_ptr<A> upA(new A());  
  70.     // std::unique_ptr<A> upAOther = upA; //It's compile error  
  71.     //std::unique_ptr<A> upAOther(upA); //It's compile error  
  72.     //std::unique_ptr<A> upAnother = std::move(upA);  
  73.  
  74.     /*  
  75.     unique_ptr(unique_ptr&& _Right)  
  76.     : _Mybase(_Right.release(),  
  77.     _STD forward<_Dx>(_Right.get_deleter()))  
  78.     {   // construct by moving _Right  
  79.     }  
  80.     */ 
  81.  
  82.     //std::unique_ptr<A> upA2(new A[2]); // It's wrong, which will have memory leak  
  83.  
  84.     // msdn for unique_ptr:  
  85.     // http://msdn.microsoft.com/en-us/library/ee410601.aspx  
  86.  
  87.     // The unique_ptr class supersedes auto_ptr, and can be used as an element of STL containers.  
  88.     /*  
  89.     std::unique_ptr<A[]> upA3(new A[2]);  
  90.  
  91.     upA3[0]->SetA(3);  
  92.     */ 
  93.  
  94.     /*  
  95.     std::unique_ptr<A> upMyA1(new A());  
  96.     std::unique_ptr<A> upMyA2(new A());  
  97.     std::vector<std::unique_ptr<A> > vecPointer;  
  98.     vecPointer.push_back(std::move(upMyA1));  
  99.     vecPointer.push_back(std::move(upMyA2));*/ 
  100.  
  101.     // It's wrong to make the auto_ptr<A> as the parameter  
  102.     // The resource gets deleted  
  103.     /*  
  104.     std::auto_ptr<A> upMyA3(new A());  
  105.     Foo(upMyA3);  
  106.     */ 
  107.  
  108.     // It's also not recommended to pass unique_ptr<A> as the parameter  
  109.     /*  
  110.     std::unique_ptr<A> upMyA4(new A());  
  111.     Foo(std::move(upMyA4));  
  112.     */ 
  113.  
  114.     // The return value can't be template  
  115.     //std::auto_ptr<A> apMyA5 = Foo();  
  116.  
  117.     // How about make the auto_ptr as the & parameter?  
  118.     // It seems can work well  
  119.     std::auto_ptr<A> apMyA6;//(new A());  
  120.     Foo2(apMyA6);  
  121.  
  122.     return 0;  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章