Boost[1]:智能指针

测试环境:win7, vs2012

 如果未安装boost,请参考:http://blog.csdn.net/alex_my/article/details/17630685

 涉及智能指针:shared_ptr, weak_ptr, scoped_ptr, auto_ptr

 其它:enable_shared_from_this

 总调用函数: testSmartPointer()

 可以将其放在main()中运行。解释在代码中。

  1. <span style="font-size:18px;">#include <string>  
  2. #include <vector>  
  3. #include <iostream>  
  4. #include <boost/scoped_ptr.hpp>  
  5. #include <boost/shared_ptr.hpp>  
  6. #include <boost/weak_ptr.hpp>  
  7. #include <boost/enable_shared_from_this.hpp>  
  8.   
  9. class Base  
  10. {  
  11. public:  
  12.     explicit Base(int a)  
  13.     : m_a(a)  
  14.     {  
  15.     }  
  16.     virtual ~Base()  
  17.     {  
  18.     }  
  19.   
  20.     int GetA() const  
  21.     {  
  22.         return m_a;   
  23.     }  
  24.   
  25. private:  
  26.     int m_a;  
  27. };  
  28.   
  29. class Derive : public Base  
  30. {  
  31. public:  
  32.     explicit Derive(int b)  
  33.         : Base(2 * b)  
  34.         , m_b(b)  
  35.     {  
  36.   
  37.     }  
  38.   
  39.     virtual ~Derive()  
  40.     {  
  41.     }  
  42.   
  43.     int GetB() const  
  44.     {  
  45.         return m_b;   
  46.     }  
  47.   
  48. private:  
  49.     int m_b;  
  50. };  
  51.   
  52. class EnableShared  
  53. {  
  54. public:  
  55.     EnableShared()  
  56.     : m_e(3)  
  57.     {  
  58.   
  59.     }  
  60.     ~EnableShared()   
  61.     {  
  62.         std::cout<< "EnableShared Destruction execute" << std::endl;  
  63.     }  
  64.   
  65.     void ShowE()  
  66.     {  
  67.         boost::shared_ptr<EnableShared> p1(this);  
  68.         std::cout<< p1->m_e << std::endl;  
  69.     }  
  70.   
  71. private:  
  72.     int m_e;  
  73. };  
  74.   
  75. class EnableSharedEx : public boost::enable_shared_from_this<EnableSharedEx>  
  76. {  
  77. public:  
  78.     EnableSharedEx()  
  79.         : m_e(3)  
  80.     {  
  81.   
  82.     }  
  83.     ~EnableSharedEx()   
  84.     {  
  85.         std::cout<< "EnableSharedEx Destruction execute" << std::endl;  
  86.     }  
  87.   
  88.     void ShowE()  
  89.     {  
  90.         //boost::shared_ptr<EnableSharedEx> p1(this);  
  91.         boost::shared_ptr<EnableSharedEx> p1 = shared_from_this();  
  92.         std::cout<< p1->m_e << std::endl;  
  93.     }  
  94.   
  95. private:  
  96.     int m_e;  
  97. };  
  98.   
  99. static void testSharedPtr();  
  100. static void testEnableSharedFromthis();  
  101. static void testScopedPtr();  
  102. static void testAutoPtr();  
  103.   
  104. void testSmartPointer()  
  105. {  
  106.     // ------------- shared_ptr -------------  
  107.     testSharedPtr();  
  108.   
  109.     // ------------- enable_shared_from_this -------------  
  110.     testEnableSharedFromthis();  
  111.   
  112.     // ------------- scoped_ptr -------------  
  113.     testScopedPtr();  
  114.   
  115.     // ------------- auto_ptr -------------  
  116.     testAutoPtr();  
  117.   
  118.     // ------------- summary -------------  
  119.     // 1 auto_ptr会转移所有权,使原拥有者失效  
  120.     // 2 shared_ptr比起auto_ptr,不会转移所有权,而是增加引用计数  
  121.     // 3 scoped_ptr不允许复制  
  122.     // 4 weak_ptr起了类似于观察者的作用,不会对拥有者造成影响  
  123. }  
  124.   
  125. void testSharedPtr()  
  126. {  
  127.     // 1 使用  
  128.     boost::shared_ptr<Base> pa(new Base(2));  
  129.     std::cout<< "testSharedPtr" << pa->GetA() << std::endl;  
  130.   
  131.     // 2 发生引用,此时pa2和pa指向同一个指针,观察计数器share_ptr::use_count_ 值从1变为2。  
  132.     boost::shared_ptr<Base> pa2 = pa;  
  133.   
  134.     // 3 弱引用,计数器并仍然是2,不过weak_count_ 从1变成了2。  
  135.     boost::weak_ptr<Base> p3 = pa;  
  136. }  
  137.   
  138. void testEnableSharedFromthis()  
  139. {  
  140.     // 1 应用举例  
  141.     boost::shared_ptr<EnableShared> pe(new EnableShared);  
  142.     //pe->ShowE();  
  143.   
  144.     // 2 注释说明  
  145.     // 编译可以通过,但是析构函数会执行两次,造成程序崩溃  
  146.     // shared_ptr的一个缺点,无法从this指针构造,无法像testSharedPtr中的引用例子一样。  
  147.   
  148.     // 3 解决办法 enable_shared_from_this,改写EnableShared为EnableSharedEx  
  149.     boost::shared_ptr<EnableSharedEx> pex(new EnableSharedEx);  
  150.     pex->ShowE();  
  151. }  
  152.   
  153. void testScopedPtr()  
  154. {  
  155.     // 1 应用举例、  
  156.     boost::scoped_ptr<Base> pb(new Base(2));  
  157.     std::cout << "testScopedPtr" << pb->GetA() << std::endl;  
  158.   
  159.     // 2 引用,无法通过编译,原因:scope_ptr不允许复制  
  160.     // boost::scoped_ptr<Base> pb2 = pb;  
  161. }  
  162.   
  163. void testAutoPtr()  
  164. {  
  165.     // 1 应用举例,与shared_ptr相似  
  166.     std::auto_ptr<Base> pa(new Base(2));  
  167.     std::cout<< "testAutoPtr: " << pa->GetA() << std::endl;  
  168.   
  169.     // 2 发生引用,与shared_ptr不同的地方在于pa编程空指针了。  
  170.     std::auto_ptr<Base> pax = pa;  
  171. }</span>  

不懂它的时候,你觉的它是洪水猛兽。了解它的时候,会觉得它是那么的亲切。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值