boost___smart_ptr

来源:互联网 发布:淘宝发货途中能退款吗 编辑:程序博客网 时间:2024/06/05 19:40

包括scoped_ptr, scoped_array, shared_ptr, shared_array, weak_ptr, intrusive_ptr六个部分。

scoped_ptr很类似std::auto_ptr,但是其所有权更加严格,一旦获得不允许转让。

复制代码
 1 namespace boost {
2 template<typename T> class scoped_ptr : noncopyable {
3 public:
4 explicit scoped_ptr(T* p = 0);
5 ~scoped_ptr();
6
7 void reset(T* p = 0);
8
9 T& operator*() const;
10 T* operator->() const;
11 T* get() const;
12
13 void swap(scoped_ptr& b);
14 };
15
16 template<typename T>
17 void swap(scoped_ptr<T> & a, scoped_ptr<T> & b);
18 }
复制代码

成员函数

explicit scoped_ptr(T* p=0)

构造函数,存储p的一份拷贝。注意,p 必须是用operator new分配的,或者是null.在构造的时候,不要求T必须是一个完整的类型。当指针p是调用某个分配函数的结果而不是直接调用new得到的时候很有用:因为这个类型不必是完整的,只需要类型T的一个前向声明就可以了。这个构造函数不会抛出异常。

~scoped_ptr()

删除指针所指向的对象。类型T在被销毁时必须是一个完整的类型。如果scoped_ptr在它被析构时并没有保存资源,它就什么都不做。这个析构函数不会抛出异常。

void reset(T* p=0);

重置一个 scoped_ptr 就是删除它已保存的指针,如果它有的话,并重新保存p. 通常,资源的生存期管理应该完全由scoped_ptr自己处理,但是在极少数时候,资源需要在scoped_ptr的析构之前释放,或者scoped_ptr要处理它原有资源之外的另外一个资源。这时,就可以用reset,但一定要尽量少用它。(过多地使用它通常表示有设计方面的问题) 这个函数不会抛出异常。

T& operator*() const;

该运算符返回一个智能指针中存储的指针所指向的对象的引用。由于不允许空的引用,所以解引用一个拥有空指针的scoped_ptr将导致未定义行为。如果不能肯定所含指针是否有效,就用函数get替代解引用。这个函数不会抛出异常。

T* operator->() const;

返回智能指针所保存的指针。如果保存的指针为空,则调用这个函数会导致未定义行为。如果不能肯定指针是否空的,最好使用函数get。这个函数不会抛出异常。

T* get() const;

返回保存的指针。应该小心地使用get,因为它可以直接操作裸指针。但是,get使得你可以测试保存的指针是否为空。这个函数不会抛出异常。get通常在调用那些需要裸指针的函数时使用。

operator unspecified_bool_type() const

返回scoped_ptr是否为非空。返回值的类型是未指明的,但这个类型可被用于Boolean的上下文(boolean context)中。在if语句中最好使用这个类型转换函数,而不要用get去测试scoped_ptr的有效性

void swap(scoped_ptr& b)

交换两个scoped_ptr的内容。这个函数不会抛出异常。

普通函数

template<typename T> void swap(scoped_ptr<T>& a,scoped_ptr<T>& b)

这个函数提供了交换两个scoped pointer的内容的更好的方法。之所以说它更好,是因为 swap(scoped1,scoped2) 可以更广泛地用于很多指针类型,包括裸指针和第三方的智能指针。scoped1.swap(scoped2) 则只能用于它的定义所在的智能指针,而不能用于裸指针。

用法

scoped_ptr的用法与普通的指针没什么区别;最大的差别在于你不必再记得在指针上调用delete,还有复制是不允许的。典型的指针操作(operator* 和 operator->)都被重载了,并提供了和裸指针一样的语法。用scoped_ptr和用裸指针一样快,也没有大小上的增加,因此它们可以广泛使用。使用boost::scoped_ptr时,包含头文件"boost/scoped_ptr.hpp". 在声明一个scoped_ptr时,用被指物的类型来指定类模板的参数。例如,以下是一个包含std::string指针的scoped_ptr:

boost::scoped_ptr<std::string> p(new std::string("Hello"));

当scoped_ptr被销毁时,它对它所拥有的指针调用delete 。

复制代码
 1 int main()
2 {
3 boost::scoped_ptr<string> sp(new string("hello world!"));
4 cout<<*sp<<endl;
5 cout<<sp->size()<<endl;
6
7 *sp = "hekexin";
8 cout<<*sp<<endl;
9 cout<<sp->size()<<endl;
10
11 //重置一个 scoped_ptr 就是删除它已保存的指针,如果它有的话,
12 //并重新保存p. 通常,资源的生存期管理应该完全由scoped_ptr自己处理,
13 //但是在极少数时候,资源需要在scoped_ptr的析构之前释放,或者scoped_ptr
14 //要处理它原有资源之外的另外一个资源。这时,就可以用reset,但一定要尽
15 //量少用它。(过多地使用它通常表示有设计方面的问题) 这个函数不会抛出异常。
16 sp.reset();
17 assert(sp.get() == 0);
18 assert(sp == 0);
19 assert(!sp);
20
21 return 0;
22 }
复制代码

 

scoped_array: 包装了new[]而不是new,为动态数组提供了代理。与scoped_ptr设计思想一致,不能赋值,拷贝,只能在被声明的作用域内使用。接口也大体相同,特点:

1. 构造函数接受的指针必须是new []的结果或者0.

2. 没有* ->重载操作符,因为持有的不是一个普通指针。

3. 提供了[]重载操作符,像普通数组一样用下表访问。

4. 没有begin(), end()等类似容器的迭代器操作。

其实还是尽量少动态分配数组,尽量用vector替代。

复制代码
int main()
{
boost::scoped_array<int> sap(new int[100]);
std::fill_n(&sap[0], 100, 5);
sap[0] = sap[2] + sap[3];

for (int i=0; i<100; ++i)
{
cout<<sap[i]<<endl;
}

return 0;
}
复制代码

shared_ptr非常有用,引用计数的引入,使其可以赋值,拷贝和任意的共享,也可以存放在容器中。

 

复制代码
 1 int main()
2 {
3 shared_ptr<int> sp(new int(10));
4 cout<<*sp<<endl;
5 assert(sp.unique());
6
7 shared_ptr<int> sp2 = sp;
8 assert(sp == sp2 && sp.use_count() == 2);
9 *sp = 100;
10 assert(!sp.unique());
11 cout<<*sp2<<endl;
12
13 sp.reset();
14 assert(!sp);
15 cout<<*sp2<<endl;
16 cout<<sp2.use_count()<<endl;
17
18 return 0;
19 }
复制代码

 

复制代码
 1 class Shared
2 {
3 private:
4 shared_ptr<int> sp_;
5
6 public:
7 Shared(shared_ptr<int> p) : sp_(p) {}
8 void print()
9 {
10 cout<<"use count : "<<sp_.use_count()<<" val : "<<*sp_<<endl;
11 }
12 void print_func(shared_ptr<int> sp)
13 {
14 cout<<"use count : "<<sp.use_count()<<" val : "<<*sp<<endl;
15 }
16
17 void print_func1(shared_ptr<int>& sp)
18 {
19 cout<<"use count : "<<sp.use_count()<<" val : "<<*sp<<endl;
20 }
21 };
22
23 int main()
24 {
25 shared_ptr<int> sp(new int(100));
26 Shared s1(sp);
27 Shared s2(sp);
28 s1.print(); // 3, 100
29 s2.print(); // 3, 100
30
31 *sp = 200;
32 s1.print(); //3, 200
33 s2.print(); //3, 200
34
35 s1.print_func(sp); // 4, 200
36
37 s1.print(); // 3, 200
38
39 s1.print_func1(sp); //3, 200
40
41 return 0;
42 }
复制代码

 

复制代码
 1 int main()
2 {
3 shared_ptr<string> sp = make_shared<string>("hello world!");
4 shared_ptr< vector<int> > spv = make_shared< vector<int> >(10, 3);
5 assert(*sp == "hello world!");
6 assert(sp.unique());
7 assert(spv->size() == 10);
8 vector<int>& vec = *spv;
9
10 typedef vector<shared_ptr<int> > vs;
11 vs spVec(10);
12 cout<<spVec.size()<<endl;
13 int i = 0;
14 for (vs::iterator ite = spVec.begin(); ite != spVec.end(); ++ite)
15 {
16 *ite = make_shared(++i);
17
18 }
19 return 0;
20 }
复制代码
0 0
原创粉丝点击