Boost库智能指针学习

参见《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 use
    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 use
    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[])
{
    //shared_ptr use
    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[])
{
    //shared_ptr应用于桥接模式
    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);
    //weak_ptr不影响引用计数
    assert(wp.use_count() == 1);

    //判断weak_ptr观察对象是否失效
    if (!wp.expired())
    {
        boost::shared_ptr<int> sp2 = wp.lock();
        *sp2 = 100;
        assert(wp.use_count() == 2);
    }
    
    assert(wp.use_count() == 1);

    //设置shared_ptr失效
    sp.reset();

    assert(wp.expired());
    //weak_ptr将获得一个空指针
    assert(!wp.lock());

    return 0;
}

6、make_shared
make_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[])
{
    //创建string的共享指针
    boost::shared_ptr<string> sp = boost::make_shared<string>("hello world");
    //创建vector的共享指针
    boost::shared_ptr<vector<int>> spv =
        boost::make_shared<vector<int>>(10, 2);
    assert(spv->size() == 10);

    cout<<*sp<<endl;
    cout<<(*spv)[0]<<endl;

    //标准容器持有shared_ptr使用
    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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值