Boost库智能指针学习

文章来源:http://blog.csdn.net/segen_jaa/article/details/7751878

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

 

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

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

1scoped_ptr

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

示例代码如下。

#include"stdafx.h"

#include<iostream>

#include<string>

#include<boost/smart_ptr.hpp>

 

using namespacestd;

using namespaceboost;

 

int _tmain(intargc, _TCHAR* argv[])

{

    //scoped_ptr use

    scoped_ptr<string> sp(newstring("hello world"));

 

    cout<<*sp<<endl;

    cout<<sp->size()<<endl;

 

    return 0;

}



2scoped_array

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

#include"stdafx.h"

#include <iostream>

#include<string>

#include<boost/smart_ptr.hpp>

 

using namespacestd;

using namespaceboost;

 

int _tmain(intargc, _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 namespacestd;

using namespaceboost;

 

int _tmain(intargc, _TCHAR* argv[])

{

    //shared_ptr use

    boost::shared_ptr<int> spi(newint(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 namespacestd;

 

class sample

{

private:

    class impl;

    boost::shared_ptr<impl> p;

public:

    sample();

    void print();

};

 

classsample::impl

{

public:

    void print()

    {

        cout<<"implprint"<<endl;

    }

};

 

sample::sample()

    : p(new impl)

{

 

}

 

voidsample::print()

{

    p->print();

}

 

int _tmain(intargc, _TCHAR* argv[])

{

    //shared_ptr应用于桥接模式

    sample s;

    s.print();

 

    return 0;

}



4shared_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 namespacestd;

 

int _tmain(intargc, _TCHAR* argv[])

{

    boost::shared_ptr<int> sp(newint(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 namespacestd;

 

int _tmain(intargc, _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;

}

 

posted @ 2014-11-14 16:38  SunkingYang  阅读(145)  评论(0编辑  收藏  举报