C++基础之boost库的智能指针

【Boost介绍】

Boost是个组织(http://www.boost.org/),提供可移植的,源码开放的C++程序库。部分Boost库已经被包含到了C++标准化委员会的TR1中。

目前可以下载到到最新boost库是1.47.0。解压后的目录结构:

使用大多数boost库不需要build,包含头文件就可以了。project 右键Properties -> C/C++ ->General ->Additional Include Directories,填入boost的根目录。(或者还需要C/C++ -> Precompiled Headers -> 选择 Not Using Precompiled Headers.)

对于那些需要build的库,比如IOStreams, Regex等(详细参考文档),如果使用的IDE是Visual Studio的话,可以从Boostpro Computing(http://www.boostpro.com/download/)下载installer安装,目前的最新版本是BoostPro 1.47.0 Installer 。

 

【Boost库的智能指针】

boost 库定义在namespace boost 中。

1. boost::scoped_ptr <boost/scoped_ptr.hpp>

跟std::auto_ptr一样用来管理单个堆内存对象,但是独享所有权,且不允许赋值和拷贝(没有重载operator=)。但是很多情况下我们都需要复制智能指针,scoped_ptr 就满足不了了。

2. boost::shared_ptr <boost/shared_ptr.hpp>

shared_ptr 是基于引用计数的智能指针,可以共享所有权,原理可参见C++基础之创建自己的智能指针

use_count():返回引用计数值。

[cpp]  view plain  copy
  1. #include <boost\shared_ptr.hpp>  
  2. #include <iostream>  
  3.   
  4. class Simple  
  5. {  
  6. public:  
  7.     Simple(int param = 0)  
  8.     {  
  9.         m_number = param;  
  10.         std::cout << "Simple: " << m_number << std::endl;  
  11.     }  
  12.   
  13.     ~Simple()  
  14.     {  
  15.         std::cout << "~Simple: " << m_number << std::endl;  
  16.     }  
  17.   
  18.     void Print()  
  19.     {  
  20.         std::cout << "Print: " << m_info.c_str() << std::endl;  
  21.     }  
  22.   
  23.     std::string m_info;  
  24.   
  25. private:  
  26.     int m_number;  
  27. };  
  28.   
  29. void Test(boost::shared_ptr<Simple> simple)  
  30. {  
  31.     simple->Print();  
  32.     std::cout << "In Test(), UseCount: " << simple.use_count() << std::endl;  
  33. }  
  34.   
  35. int _tmain(int argc, _TCHAR* argv[])  
  36. {  
  37.     boost::shared_ptr<Simple> mySimple(new Simple(1));  
  38.     if (mySimple.get()) //访问裸指针。由于智能指针是一个对象,所以if(smart_object)永远为真。  
  39.     {  
  40.         mySimple->Print();  
  41.         mySimple.get()->m_info = "Hello ";  
  42.         mySimple->Print();  
  43.         (*mySimple).m_info += "World";  
  44.         mySimple->Print();  
  45.     }  
  46.   
  47.     std::cout << "1. UseCount: " << mySimple.use_count() << std::endl;  
  48.     Test(mySimple);  
  49.     std::cout << "2. UseCount: " << mySimple.use_count() << std::endl;  
  50.   
  51.     return 0;  
  52. }  

执行结果:

在Test()函数内部,引用计数为2。

3. boost::scoped_array <boost/scoped_array.hpp>

用于管理动态数组,跟scoped_ptr一样,也是独享所有权。

 4. boost::shared_array <boost/shared_array.hpp>

用于管理动态数组,跟boost::shared_ptr一样,内部使用了引用计数。建议使用std::vector<shared_ptr>,而不是shared_array。

 5. boost::weak_ptr <boost/weak_ptr.hpp>

boost::weak_ptr 是 boost::shared_ptr 的观察者,只对后者进行引用而不改变引用计数,当被观察的boost::shared_ptr失效后,相应的boost::weak_ptr 也失效。boost::weak_ptr 主要用在软件架构设计中,可以在基类中定义一个 boost::weak_ptr,指向子类的 boost::shared_ptr,这样基类通过观察自己的 boost::weak_ptr 是否为空就知道子类有没对自己赋值了,不会影响子类 boost::shared_ptr 的引用计数,用以降低复杂度,更好的管理对象。boost::weak_ptr 也用来解决boost::shared_ptr的循环引用问题。

[cpp]  view plain  copy
  1. <p>#include <boost\shared_ptr.hpp>  
  2. #include <boost\weak_ptr.hpp>  
  3. #include <iostream></p><p>  
  4.     boost::weak_ptr<Simple> weakSimple;  
  5.     boost::shared_ptr<Simple> mySimple(new Simple(1));  
  6.   
  7.     std::cout << "boost::shared_ptr UseCount: " << mySimple.use_count() << std::endl;  
  8.     weakSimple = mySimple;  
  9.     std::cout << "boost::shared_ptr UseCount: " << mySimple.use_count() << std::endl;</p>  

尽管做了赋值,但是内部的引用计数没有变化。

6. boost::intrusive_ptr <boost/intrusive_ptr.hpp>

插入式智能指针,内部不含有引用计数,需要程序员自己加入,不然编译不过。

 

【参考】

http://club.topsage.com/thread-2276515-1-1.html

http://blog.csdn.net/xt_xiaotian/article/details/5714477

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值