boost智能指针笔记

来源:互联网 发布:mac用的办公软件 编辑:程序博客网 时间:2024/06/15 20:04


参见《Boost程序库完全开放指南》 第3章 内存管理


所有示例,采用vs2010开发工具(vs2005也适用),均为win32控制台程序。

Boost库的配置可参照:http://blog.csdn.NET/segen_jaa/article/details/7407404。

1、scoped_ptr

内动态管理内存。但所有权不能转让,不能进行赋值操作。

示例代码如下。

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <string>  
  4. #include <boost/smart_ptr.hpp>  
  5.   
  6. using namespace std;  
  7. using namespace boost;  
  8.   
  9. int _tmain(int argc, _TCHAR* argv[])  
  10. {  
  11.     //scoped_ptr use  
  12.     scoped_ptr<string> sp(new string("hello world"));  
  13.   
  14.     cout<<*sp<<endl;  
  15.     cout<<sp->size()<<endl;  
  16.   
  17.     return 0;  
  18. }  

2、scoped_array

包装了new[]操作符,为动态数组提供了一个代理。但所有权不能转让。
示例代码如下。

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <string>  
  4. #include <boost/smart_ptr.hpp>  
  5.   
  6. using namespace std;  
  7. using namespace boost;  
  8.   
  9. int _tmain(int argc, _TCHAR* argv[])  
  10. {  
  11.     //scoped_array use  
  12.     scoped_array<int> sa(new int[100]);  
  13.   
  14.     fill_n(&sa[0], 100, 5);  
  15.     sa[10] = sa[20] + sa[30];  
  16.   
  17.     cout<<sa[10]<<endl;  
  18.   
  19.     return 0;  
  20. }  

3、shared_ptr

最有价值最有用的智能指针。封装内存创建释放,可自由拷贝和赋值,能安全地放到标准容器中。

弥补auto_ptr因为转义语义不能作为STL容器元素的缺陷。

PS:采用vs2010时,发现shared_ptr已经是新的标准库的一个主要成员。
示例代码如下。

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <string>  
  4. #include <boost/smart_ptr.hpp>  
  5.   
  6. using namespace std;  
  7. using namespace boost;  
  8.   
  9. int _tmain(int argc, _TCHAR* argv[])  
  10. {  
  11.     //shared_ptr use  
  12.     boost::shared_ptr<int> spi(new int(10));  
  13.     assert(spi.unique());  
  14.     *spi = 253;  
  15.   
  16.     cout <<*spi<<endl;  
  17.     cout<<spi.use_count()<<endl;  
  18.   
  19.     return 0;  
  20. }  


另,桥接器模式的应用。

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <boost/smart_ptr.hpp>  
  4.   
  5. using namespace std;  
  6.   
  7. class sample  
  8. {  
  9. private:  
  10.     class impl;  
  11.     boost::shared_ptr<impl> p;  
  12. public:  
  13.     sample();  
  14.     void print();  
  15. };  
  16.   
  17. class sample::impl  
  18. {  
  19. public:  
  20.     void print()  
  21.     {  
  22.         cout<<"impl print"<<endl;  
  23.     }  
  24. };  
  25.   
  26. sample::sample()  
  27.     : p(new impl)  
  28. {  
  29.   
  30. }  
  31.   
  32. void sample::print()  
  33. {  
  34.     p->print();  
  35. }  
  36.   
  37. int _tmain(int argc, _TCHAR* argv[])  
  38. {  
  39.     //shared_ptr应用于桥接模式  
  40.     sample s;  
  41.     s.print();  
  42.   
  43.     return 0;  
  44. }  

4、shared_array

使用类似shared_ptr,包装了new[]操作符。使用引用计数机制为动态数组提供一个代理。
说明:shared_array不提供索引的范围检查,建议使用shared_ptr<vector>或vector<shared_ptr>来代替。

5、weak_ptr

shared_ptr的助手,协助shared_ptr工作。不增加引用计数,不对资源进行操作,作为一个静静的观察者。

常用方法,用lock()从被观测的shared_ptr获得一个可用的shared_ptr对象,从而操作资源。

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <boost/smart_ptr.hpp>  
  4.   
  5. using namespace std;  
  6.   
  7. int _tmain(int argc, _TCHAR* argv[])  
  8. {  
  9.     boost::shared_ptr<int> sp(new int(10));  
  10.     assert(sp.use_count() == 1);  
  11.   
  12.     boost::weak_ptr<int> wp(sp);  
  13.     //weak_ptr不影响引用计数  
  14.     assert(wp.use_count() == 1);  
  15.   
  16.     //判断weak_ptr观察对象是否失效  
  17.     if (!wp.expired())  
  18.     {  
  19.         boost::shared_ptr<int> sp2 = wp.lock();  
  20.         *sp2 = 100;  
  21.         assert(wp.use_count() == 2);  
  22.     }  
  23.       
  24.     assert(wp.use_count() == 1);  
  25.   
  26.     //设置shared_ptr失效  
  27.     sp.reset();  
  28.   
  29.     assert(wp.expired());  
  30.     //weak_ptr将获得一个空指针  
  31.     assert(!wp.lock());  
  32.   
  33.     return 0;  
  34. }  

6、make_shared
make_shared工厂函数代替new操作符。

示例代码如下。

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <string>  
  4. #include <vector>  
  5. #include <boost/smart_ptr.hpp>  
  6. #include <boost/make_shared.hpp>  
  7.   
  8. using namespace std;  
  9.   
  10. int _tmain(int argc, _TCHAR* argv[])  
  11. {  
  12.     //创建string的共享指针  
  13.     boost::shared_ptr<string> sp = boost::make_shared<string>("hello world");  
  14.     //创建vector的共享指针  
  15.     boost::shared_ptr<vector<int>> spv =  
  16.         boost::make_shared<vector<int>>(10, 2);  
  17.     assert(spv->size() == 10);  
  18.   
  19.     cout<<*sp<<endl;  
  20.     cout<<(*spv)[0]<<endl;  
  21.   
  22.     //标准容器持有shared_ptr使用  
  23.     typedef vector<boost::shared_ptr<int>> vs;  
  24.     vs v(10);  
  25.   
  26.     int i = 0;  
  27.     vs::iterator vIter = v.begin();  
  28.     for (; vIter != v.end(); ++vIter)  
  29.     {  
  30.         (*vIter) = boost::make_shared<int>(++i);  
  31.         cout<<*(*vIter)<<", ";  
  32.     }  
  33.     cout<<endl;  
  34.   
  35.     boost::shared_ptr<int> p = v[9];  
  36.     *p = 100;  
  37.     cout<<*v[9]<<endl;  
  38.   
  39.     return 0;  
  40. }  




1 0
原创粉丝点击