目录:

标准库中只提供两大类型指针:<头文件 memory>

           1.共享式<shard_ptr>.辅助类<weak_ptr,bad_weak_ptr,enable_shared_from_this>

           2.独占式<unique_ptr>.

应该摒弃C++98提供的智能指针auto_ptr.

<误区与切记:定义智能指针智能通过构造函数初始化或者调用成员方法>

shard_ptr<string> p;

p = new string("hello");错误用法

p.reset(new string("hello"));正确用法


1.共享式<shard_ptr>(注意,一般指向对象.指向数组的话,要自定义delete并且这里还给出了unique_ptr释放array的方法)

①自定义delete:

STL向导阅读②----智能指针

②关于Array和自定义delete的方法:

STL向导阅读②----智能指针


②weak_ptr

weak_ptr为shard_ptr的辅助类指针,解决回环问题.<不需要你理解什么是回环问题,如果你代码跟下面一样相互引用的话就用weak_ptr代替weak_ptr>

weak_ptr主要是配合share_ptr来实现应用计数,同时不会发生循环引用。

参考:http://www.cnblogs.com/TianFang/archive/2008/09/20/1294590.html

[cpp] view plain copy
 print?
  1. #include "boost/smart_ptr.hpp"  
  2. #include "boost/weak_ptr.hpp"  
  3.   
  4. #include<iostream>  
  5. using namespace std;  
  6. using namespace boost;  
  7.   
  8. class node;  
  9. typedef boost::weak_ptr<node> ptr_type;  
  10.   
  11. class node  
  12. {  
  13. public:  
  14.     node();   
  15.     ~node();  
  16.     int value_;  
  17.     ptr_type next;  
  18. };  
  19.   
  20. node::node()  
  21. {  
  22.     cout<<"node()"<<endl;  
  23. }  
  24.   
  25. node::~node()  
  26. {  
  27.     cout<<"~node()"<<endl;  
  28. }  
  29.   
  30. int main(int,char**)  
  31. {  
  32.     auto p1 = boost::make_shared<node>();  
  33.     auto p2 = boost::make_shared<node>();  
  34.     p1->next = p2;//weak_ptr(const share_ptr&) //不增加引用计数  
  35.     p2->next = p1;//weak_ptr(const share_ptr&) //不增加引用计数  
  36.   
  37.     return 0;  
  38. };  

程序输出:

node()
node()
~node()
~node()
请按任意键继续. . .

导致循环引用的情形

[cpp] view plain copy
 print?
  1. #include "boost/smart_ptr.hpp"  
  2. #include "boost/weak_ptr.hpp"  
  3.   
  4. #include<iostream>  
  5. using namespace std;  
  6. using namespace boost;  
  7.   
  8. class node;  
  9. //typedef boost::weak_ptr<node> ptr_type;  
  10. typedef boost::shared_ptr<node> ptr_type;//这样会发生循环引用  
  11.   
  12. class node  
  13. {  
  14. public:  
  15.     node();   
  16.     ~node();  
  17.     int value_;  
  18.     ptr_type next;//是否会循环引用取决于ptr_type的类型  
  19. };  
  20.   
  21. node::node()  
  22. {  
  23.     cout<<"node()"<<endl;  
  24. }  
  25.   
  26. node::~node()  
  27. {  
  28.     cout<<"~node()"<<endl;  
  29. }  
  30.   
  31. int main(int,char**)  
  32. {  
  33.     auto p1 = boost::make_shared<node>();  
  34.     auto p2 = boost::make_shared<node>();  
  35.     p1->next = p2;//weak_ptr(const share_ptr&) //不增加引用计数  
  36.     p2->next = p1;//weak_ptr(const share_ptr&) //不增加引用计数  
  37.   
  38.     return 0;  
  39. };  
程序输出(只有创建对象,没有释放对象):
node()
node()
请按任意键继续. . .

STL向导阅读②----智能指针


2..独占式<unique_ptr>.

STL向导阅读②----智能指针

STL向导阅读②----智能指针

插讲: 

C++11中使用shared_ptr和unique_ptr管理动态数组

C++11中,若使用shared_ptr管理一个动态数组,则需手动制定一个删除器。

auto sp = std::shared_ptr(new int[len], [](char *p){delete []p;});

但是这样每次手动指定有点麻烦,经过查阅资料,发现可以使用shared_ptr为动态数组创建一个工厂函数。

具体使用如下:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <memory>  
  3. #include <string.h>  
  4. using namespace std;  
  5.   
  6. template <typename T>  
  7. shared_ptr<T> make_shared_array(size_t size)  
  8. {  
  9.     //default_delete是STL中的默认删除器  
  10.     return shared_ptr<T>(new T[size], default_delete<T[]>());  
  11. }  
  12.   
  13. int main()  
  14. {  
  15.     auto sp_array = make_shared_array<char>(100);  
  16.     strcpy(sp_array.get(), "hello smart pointer");  
  17.     sp_array.get()[0] = 'a';  
  18.     cout << sp_array << endl;  
  19.   
  20.     //使用原始指针完成相同的功能:  
  21.     auto str_array = new char[100];  
  22.     strcpy(str_array, "hello old pointer");  
  23.     str_array[0] = 'a';  
  24.     cout << str_array << endl;  
  25.     delete [] str_array;  
  26.   
  27.     return 0;  
  28. }  

输出结果为:

aello smart pointer
aello old pointer

需要注意的是:我们常常需要对动态数组中的某一个元素进行操作,但shared_ptr没有提供[]操作符。

不过我们可以使用 sp.get()先获取原始指针,再对原始指针进行下标操作。

而unique_ptr对动态数组提供了支持,指定删除器是一个可选项。也可以直接使用下标操作:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <memory>  
  3. #include <string.h>  
  4. using namespace std;  
  5.   
  6. class mclass  
  7. {  
  8. public:  
  9.     mclass()  
  10.     {  
  11.         mem = new char[100];  
  12.         cout << "mclass constructor" << endl;  
  13.     }  
  14.   
  15.     ~mclass()  
  16.     {  
  17.         delete [] mem;  
  18.         cout << "mclass deconstrucotr" << endl;  
  19.     }  
  20. public:  
  21.     char *mem;  
  22. };  
  23.   
  24.   
  25. int main ()  
  26. {  
  27.     std::unique_ptr<mclass[]> up (new mclass[2]);  
  28.     strcpy(up[0].mem, "hello unique_ptr");  
  29.     cout << up[0].mem << endl;  
  30.     return 0;  
  31. }  

程序的输出为:

mclass constructor
mclass constructor
hello unique_ptr
mclass deconstrucotr
mclass deconstrucotr

PS:智能指针只能释放它自己直接指向的内存,若上段代码中mclass类的析构函数中忘了对mem进行释放,依然会造成内存泄露。

原文转载自:http://blog.csdn.net/wks19891215/article/details/50992171


相关文章: