c++11 stl 学习之 shared_ptr

shared_ptr
智能指针 shared_ptr 的声明初始化方式
由于指针指针使用explicit参数 必须显示声明初始化
shared_ptr<string> pNico = new string("nico"); // ERROR
shared_ptr<string> pNico{new string("nico")}; // OK

也可以使用make_shared()
shared_ptr<string> pNico = make_shared<string>("nico");
shared_ptr<string> pJutta = make_shared<string>("jutta");

智能指针一旦声明
就不能再次分配 除非使用reset()
shared_ptr<string> pNico4;
pNico4 = new string("nico");
//ERROR: no assignment for ordinary pointers
pNico4.reset(new string("nico")); // OK

shared_ptr的使用方式与实际指针使用类似 基本没什么区别

示例 sharedPtrTest1()

//=======================================
对于shared_ptr中的参数 可以指定 删除器 Deleter
示例 sharedPtrTest2()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <fstream> //for ofstream
#include <cstdio> //for remove()
 
using namespace std;
 
 
void sharedPtrTest1()
{
    shared_ptr<string> pNico(new string("nico"));
    shared_ptr<string> pJutta(new string("jutta"));
 
    (*pNico)[0] = 'N';
    pJutta->replace(0, 1, "J");
 
    vector<shared_ptr<string>> whoMadeCoffee;
    whoMadeCoffee.push_back(pJutta);
    whoMadeCoffee.push_back(pJutta);
    whoMadeCoffee.push_back(pNico);
    whoMadeCoffee.push_back(pJutta);
    whoMadeCoffee.push_back(pNico);
 
    for (auto ptr : whoMadeCoffee) {
        cout << *ptr << " ";
    }
    cout << endl;
    // overwrite a name again
    *pNico = "Nicolai";
    // print all elements again
    for (auto ptr : whoMadeCoffee) {
        cout << *ptr << " ";
    }
    cout << endl;
    // print some internal data
    cout << "use_count: " << whoMadeCoffee[0].use_count() << endl;
}
 
 
class FileDeleter
{
private:
    std::string filename;
public:
    FileDeleter(const std::string& fn)
        : filename(fn) {
    }
    void operator () (std::ofstream* fp) {
        fp->close(); //close.file
        std::remove(filename.c_str()); //delete file
        cout << "delete file finish" << endl;
    }
};
 
void sharedPtrTest2()
{
    shared_ptr<std::ofstream> fp(new std::ofstream("tmpfile.txt"),
        FileDeleter("tmpfile.txt"));
}
 
 
 
 
int _tmain(int argc, _TCHAR* argv[])
{
    sharedPtrTest1();
    sharedPtrTest2();
 
    return 0;
}

  

 shared_ptr的误用

下面是错误示例

int* p = new int;

shared_ptr<int> sp1(p);

shared_ptr<int> sp2(p);

这将产生错误 对于int指针有两个智能指针的内存管理器对其进行管理

sp1 sp2 都对其关联的资源进行释放 

正确的代码如下:

shared_ptr<int> sp1(new int);

shared_ptr<int> sp2(sp1);

 

还有一种隐蔽的情况 也会发生这种错误:

比如在Person 中增加一个函数setParentsAndTheirKids()

void setParentsAndTheirKids(shared_ptr<Person> m = nullptr,
shared_ptr<Person> f = nullptr)
{
mother = m;
father = f;
if (m != nullptr){
m->kids.push_back(shared_ptr<Person>(this));
}
if (f != nullptr){
f->kids.push_back(shared_ptr<Person>(thid));
}
}

int _tmain(int argc, _TCHAR* argv[])
{
string name("nico");
shared_ptr<Person> mom(new Person(name + "'s mom"));
shared_ptr<Person> dad(new Person(name + "'s dad"));
shared_ptr<Person> kid(new Person(name + "name"));

kid->setParentsAndTheirKids(mom,dad);

return 0;
}

 

但是如果在自己的类中使用包含this的shared_ptr指针 会增加引用计数

导致无法释放

所以引进enable_shared_from_this类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
 
 
class Person : public std::enable_shared_from_this<Person> {
public:
    string name;
    shared_ptr<Person> mother;
    shared_ptr<Person> father;
    //vector<shared_ptr<Person>> kids;
    vector<weak_ptr<Person>> kids;
 
    Person(const string& n,
        shared_ptr<Person> m = nullptr,
        shared_ptr<Person> f = nullptr)
        :name(n), mother(m), father(f){
    };
 
    ~Person(){
        cout << "delete " << name << endl;
    }
 
    //====================================
    void setParentsAndTheirKids(shared_ptr<Person> m = nullptr,
        shared_ptr<Person> f = nullptr)
    {
        mother = m;
        father = f;
        if (m != nullptr){
            m->kids.push_back(shared_ptr<Person>(shared_from_this()));
        }
        if (f != nullptr){
            f->kids.push_back(shared_ptr<Person>(shared_from_this()));
        }
    }
};
 
 
 
 
 
int _tmain(int argc, _TCHAR* argv[])
{
    string name("nico");
    shared_ptr<Person> mom(new Person(name + "'s mom"));
    shared_ptr<Person> dad(new Person(name + "'s dad"));
    shared_ptr<Person> kid(new Person(name + "name"));
 
    kid->setParentsAndTheirKids(mom,dad);
 
    return 0;
}

  

 

posted on   itdef  阅读(1186)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示