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
内动态管理内存。但所有权不能转让,不能进行赋值操作。
示例代码如下。
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <boost/smart_ptr.hpp>
-
- using namespace std;
- using namespace boost;
-
- int _tmain(int argc, _TCHAR* argv[])
- {
-
- scoped_ptr<string> sp(new string("hello world"));
-
- cout<<*sp<<endl;
- cout<<sp->size()<<endl;
-
- return 0;
- }
2、scoped_array
包装了new[]操作符,为动态数组提供了一个代理。但所有权不能转让。
示例代码如下。
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <boost/smart_ptr.hpp>
-
- using namespace std;
- using namespace boost;
-
- int _tmain(int argc, _TCHAR* argv[])
- {
-
- scoped_array<int> sa(new int[100]);
-
- fill_n(&sa[0], 100, 5);
- sa[10] = sa[20] + sa[30];
-
- cout<<sa[10]<<endl;
-
- return 0;
- }
3、shared_ptr最有价值最有用的智能指针。封装内存创建释放,可自由拷贝和赋值,能安全地放到标准容器中。
弥补auto_ptr因为转义语义不能作为STL容器元素的缺陷。
PS:采用vs2010时,发现shared_ptr已经是新的标准库的一个主要成员。
示例代码如下。
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <boost/smart_ptr.hpp>
-
- using namespace std;
- using namespace boost;
-
- int _tmain(int argc, _TCHAR* argv[])
- {
-
- boost::shared_ptr<int> spi(new int(10));
- assert(spi.unique());
- *spi = 253;
-
- cout <<*spi<<endl;
- cout<<spi.use_count()<<endl;
-
- return 0;
- }
另,桥接器模式的应用。
- #include "stdafx.h"
- #include <iostream>
- #include <boost/smart_ptr.hpp>
-
- using namespace std;
-
- class sample
- {
- private:
- class impl;
- boost::shared_ptr<impl> p;
- public:
- sample();
- void print();
- };
-
- class sample::impl
- {
- public:
- void print()
- {
- cout<<"impl print"<<endl;
- }
- };
-
- sample::sample()
- : p(new impl)
- {
-
- }
-
- void sample::print()
- {
- p->print();
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
-
- sample s;
- s.print();
-
- return 0;
- }
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对象,从而操作资源。
- #include "stdafx.h"
- #include <iostream>
- #include <boost/smart_ptr.hpp>
-
- using namespace std;
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- boost::shared_ptr<int> sp(new int(10));
- assert(sp.use_count() == 1);
-
- boost::weak_ptr<int> wp(sp);
-
- assert(wp.use_count() == 1);
-
-
- if (!wp.expired())
- {
- boost::shared_ptr<int> sp2 = wp.lock();
- *sp2 = 100;
- assert(wp.use_count() == 2);
- }
-
- assert(wp.use_count() == 1);
-
-
- sp.reset();
-
- assert(wp.expired());
-
- assert(!wp.lock());
-
- return 0;
- }
6、make_sharedmake_shared工厂函数代替new操作符。示例代码如下。
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <vector>
- #include <boost/smart_ptr.hpp>
- #include <boost/make_shared.hpp>
-
- using namespace std;
-
- int _tmain(int argc, _TCHAR* argv[])
- {
-
- boost::shared_ptr<string> sp = boost::make_shared<string>("hello world");
-
- boost::shared_ptr<vector<int>> spv =
- boost::make_shared<vector<int>>(10, 2);
- assert(spv->size() == 10);
-
- cout<<*sp<<endl;
- cout<<(*spv)[0]<<endl;
-
-
- typedef vector<boost::shared_ptr<int>> vs;
- vs v(10);
-
- int i = 0;
- vs::iterator vIter = v.begin();
- for (; vIter != v.end(); ++vIter)
- {
- (*vIter) = boost::make_shared<int>(++i);
- cout<<*(*vIter)<<", ";
- }
- cout<<endl;
-
- boost::shared_ptr<int> p = v[9];
- *p = 100;
- cout<<*v[9]<<endl;
-
- return 0;
- }
1 0