-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcpp11_17_shared_weak.cpp
More file actions
36 lines (31 loc) · 1.12 KB
/
cpp11_17_shared_weak.cpp
File metadata and controls
36 lines (31 loc) · 1.12 KB
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
/************************************************************************
* 代码标签 : 测试shared_ptr与weak_ptr
* 功能描述 :
* 类 名 称 :
* 版 本 号 : v1.0
* 说 明 :
* 作 者 : dragonfive
* 创建时间 :
* 更新时间 :
************************************************************************
* Copyright @dragonfive 2016 . All rights reserved.
************************************************************************/
#include <memory>
#include <iostream>
using namespace std;
int main()
{
//shared_ptr<string> sp1(make_shared<string>("Hello"));//make_shared用一个变量(而不是指针哦)生成一个shared指向的内存管理对象;
//shared_ptr<string> sp1(make_shared<string>(string("Hello")));
shared_ptr<string> sp1(new string("Hello"));
shared_ptr<string> sp2 = sp1;
cout << "*sp1:" << *sp1 << endl;
cout << "*sp2:" << *sp2 << endl;
sp1.reset();
cout << "*sp2:" << *sp2 << endl;
weak_ptr<string> wp = sp2;
cout << "*wp.lock():" << *wp.lock() << endl;
sp2.reset();
//cout << "*wp.lock():" << *wp.lock() << endl; //! 运行时会出错
return 0;
}