Boost[1]:智能指针

来源:互联网 发布:windows 10 mobile壁纸 编辑:程序博客网 时间:2024/06/06 04:04

测试环境: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()中运行。解释在代码中。

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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>  

不懂它的时候,你觉的它是洪水猛兽。了解它的时候,会觉得它是那么的亲切。
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果6总是耳机模式怎么办 苹果没有插耳机模式怎么办 苹果手机切换耳机模式怎么办 苹果6s出现耳机模式怎么办 苹果6变成了耳机模式怎么办 苹果手机成耳机模式了怎么办 华为mate8耳机声音小怎么办 移动sim卡丢了怎么办 蓝牙耳机开不开机怎么办 苹果手机蓝牙不匹配怎么办 苹果6蓝牙坏了怎么办 蓝牙密钥不正确不匹配怎么办 华为p6开不了机怎么办 华为c199手机不停重启怎么办 华为手机用户数据被锁定怎么办 朵唯手机丢了怎么办 网件r6220穿墙差怎么办 无线网打王者卡怎么办 酷翼x9忘了密码怎么办 楼上的路由器楼下不好使怎么办 电信4g网络不好怎么办 农村只有2g网怎么办 电信卡4g网速慢怎么办 小米手机触屏失灵怎么办 荣耀v10电信网速很慢怎么办 华为路由器掉线了怎么办 三星s6只识别一张卡怎么办 华为手机卡不显示了怎么办 华为账号手机卡丢了怎么办 荣耀8耗电量太快怎么办 vivo卡2不显示怎么办 电信宽带玩王者荣耀卡怎么办 联通4g玩游戏卡怎么办 华为悦盒遥控器丢了怎么办 6s不能用电信卡怎么办 iphone6电信卡无服务怎么办 魅族手机电信卡怎么办 小米手机读不出sim卡怎么办 魅蓝note6耗电快怎么办 oppo手机下载密码忘了怎么办 华为v9玩飞车卡怎么办