C++2.0新特性(六)——<Smart Pointer(智能指针)之shared_ptr>

  Smart Pointer(智能指针)指的是一类指针,并不是单一某一个指针,它能知道自己被引用的个数以至于在最后一个引用消失时销毁它指向的对象,本文主要介绍C++2.0提供的新东西

一、Smart Pointer分类

  C++2.0提供了两大类型的智能指针,该模块都被定义于头文件<memory>:

    

 二、标准库提供的智能指针类

2.1 class shared_ptr

  提供了共享式拥有语义,也就是说当对个shared_ptr可以共享(或拥有)同一个对象,对象的最后一个拥有者有责任销毁对象,并清理与该对象相关的所有资源,也就是说它所指向的对象不再被需要时,自动释放(当超出作用域时,其析构函数被调用,在析构函数中,将其引用计数减1,如果引用计数的值变为0,则删除关联的原始指针,默认使用delete释放内存)与对象相关的资源。

2.2 shared_ptr使用

  可以像其他任何指针一样使用,可以赋值、拷贝、比较它们也可以使用*和->操作符访问所指向的内容

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <memory>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     // two shared pointers representing two persons by their name
10     shared_ptr<string> pNico(new string("nico"));//shared_ptr<string> pNico = make_shared<string>("nico");
11     shared_ptr<string> pJutta(new string("jutta"));//注意,直接这种形式的构造表示这个构造函数是explicit
12 
13     // capitalize person names
14     (*pNico)[0] = 'N';
15     pJutta->replace(0, 1, "J");
16 
17     // put them multiple times in a container
18     vector<shared_ptr<string>> whoMadeCoffee;//此处放进去的是指针的引用,不是指针的内容
19     whoMadeCoffee.push_back(pJutta);
20     whoMadeCoffee.push_back(pJutta);
21     whoMadeCoffee.push_back(pNico);
22     whoMadeCoffee.push_back(pJutta);
23     whoMadeCoffee.push_back(pNico);
24 
25     // print all elements
26     for (auto ptr : whoMadeCoffee) {
27         cout << *ptr << "  ";
28     }
29     cout << endl;
30 
31     // overwrite a name again
32     *pNico = "Nicolai";
33 
34     // print all elements again
35     for (auto ptr : whoMadeCoffee) {
36         cout << *ptr << "  ";
37     }
38     cout << endl;
39 
40     // print some internal data
41     cout << "use_count: " << whoMadeCoffee[0].use_count() << endl;
42     //use_count 返回第一个元素被引用的次数,容器里面的三个拷贝和自身,加起来为4
43 }

 2.2 shared_ptr自定义析构函数

  再上图例子中,如果我们想自己定义析构函数的规则,不想用string*默认的,那我们就需要在构造时增加一点改动:

传递一个lambda作为shared_ptr构造函数的第二实参,这样申明的方式当其最末一个拥有者被摧毁时,会调用这个lambda

1 shared_ptr<string> pNico(new string("nico"), [](string* p) {
2         cout << "delete" << *p << endl;
3         delete p; });

 2.3 shared_ptr对于数组的使用

  一般我们使用shared_ptr的时候没有指定其析构函数,是因为shared_ptr有一个默认的析构函数,这个默认的析构函数调用的是delete函数,这就意味着shared_ptr拥有的是由new建立起来的单一对象时,default delete才能适用,但是当我们使用数组的时候(数组需要delete[]),这个默认的delete就不适用了,我们需要自己定义delete.

std::shared_ptr<int> p(new int[10], [](int *p) {delete[] p; });

也可使用编译器为unique_ptr提供的辅助函数作为deleter析构策略:

std::shared_ptr<int> p(new int[10], std::default_delete<int[]>());

注意:shared_ptr不提供operator [],只提供operator*和operator->,想访问内存,必须使用get()函数来获取被shared_ptr包裹的内部指针;

2.4 shared_ptr析构策略

  当shared_ptr的最后一个声明周期结束后,如果清理工作不仅仅是删除内存,这时你必须明确给出自己的deleter,可以指定属于自己的析构策略。

  下例展示:当指向临时文件的的最后一个引用消失时,我们要删除这个文件

 1 #include <string>
 2 #include <fstream>   // for ofstream
 3 #include <memory>    // for shared_ptr
 4 #include <cstdio>    // for remove()
 5 
 6 class FileDeleter
 7 {
 8   private:
 9     std::string filename;
10   public:
11     FileDeleter (const std::string& fn)
12      : filename(fn) {
      std::cout << "constructor" << std::endl;
13 } 14 void operator () (std::ofstream* fp) {
       std::cout << "delete" << std::endl;
15 delete fp; // close file 16 std::remove(filename.c_str()); // delete file 删除文件 17 } 18 }; 19 20 int main() 21 { 22 // create and open temporary file:

//这里会创建一个shared_ptr指针,令他指向new新建的输出文件,FileDeleter将负责shared_ptr的最后一个拷贝失去此输出文件的所有权时进行一系列清理操作

23     std::shared_ptr<std::ofstream> fp(new std::ofstream("tmpfile.txt"),
24                                       FileDeleter("tmpfile.txt"));

//解释上一行的运行原理:首先new时会产生一个临时对象,FileDeleter("tmpfile.txt")也会产生一个临时对象,当析构时,会将new的临时对象传给FileDeleter,即调用()运算符

      std::cout << "reset" << std::endl;

      fp.reset();
      std::cout << "reset end" << std::endl;

25 //... 26 }

 2.5 常见成员函数以及分类介绍

 

原文地址:https://www.cnblogs.com/laiyingpeng/p/11662594.html