scoped_ptr和scoped_array

作者在 2014-02-01 10:22:42 发布以下内容

     scoped_ptr不允许拷贝、赋值(因为scoped_ptr同时把拷贝构造函数和赋值操作符都声明为私有的,禁止对智能指针的复制操作),只能在scoped_ptr被声明的作用域使用。除了*和->外也没有定义其他的操作符(不能对scoped_ptr进行++或者--等指针算术操作)。

示例如下:

#include <boost/smart_ptr.hpp>

#include <iostream>
#include <string>
using namespace boost;
using namespace std;
int main()
{
typedef scoped_ptr<string> s_ptr;
s_ptr sp(new string("text"));
cout << *sp << endl;
cout << sp->size() << endl;

    scoped_array与scoped_ptr源于相同的设计思想,故而用法非常相似。

示例如下 :

#include <boost/smart_ptr.hpp>
#include <iostream>
#include <string>
using namespace boost;
using namespace std;
int main()
{
int *arr = new int[100];
scoped_array<int> sa(arr);
fill_n(&sa[0], 100, 5);
sa[10] = sa[20] + sa[30];
cout << sa[10] << endl;
return 0;
}


return 0;
}
boost库程序设计 | 阅读 2629 次
文章评论,共0条
游客请输入验证码