-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharedPtr.cpp
137 lines (105 loc) · 3 KB
/
SharedPtr.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "SharedPtr.h"
#include <memory>
#include <iostream>
//using std::auto_ptr;
using std::unique_ptr;
using std::make_unique;
using std::shared_ptr;
using std::make_shared;
using std::cout;
using std::endl;
unique_ptr<int> func(int val)
{
unique_ptr<int> up(new int(val));
return up;
}
void SharedPtr::test()
{
////std::auto_ptr
////初始化方式1
//auto_ptr<int> ap1(new int(8));
//cout << " *ap1: " << *ap1 << " ap1.get(): " << ap1.get() << endl;
////初始化方式2
//auto_ptr<int> ap2;
//ap2.reset(new int(8));
//cout << " *ap2: " << *ap2
// << " ap2.get(): " << ap2.get() << endl;
////测试拷贝构造
//auto_ptr<int> ap3(new int(2));
//auto_ptr<int> ap4(ap3);
//if (nullptr != ap3.get())
//{
// cout << "ap3 is not nullptr" << endl;
//}else {
// cout << "ap3 is nullptr" << endl;
//}
//if (nullptr != ap4.get())
//{
// cout << "ap4 is not nullptr" << endl;
//}else {
// cout << "ap4 is nullptr" << endl;
//}
////测试赋值构造
//auto_ptr<int> ap5(new int(5));
//auto_ptr<int> ap6 = ap5;
//if (nullptr != ap5.get())
//{
// cout << "ap5 is not nullptr" << endl;
//}
//else {
// cout << "ap5 is nullptr" << endl;
//}
//if (nullptr != ap6.get())
//{
// cout << "ap6 is not nullptr" << endl;
//}
//else {
// cout << "ap6 is nullptr" << endl;
//}
//-----------------------------------------------
//std::unique_ptr
//初始化方式1
unique_ptr<int> usp1(new int(23));
//初始化方式2
unique_ptr<int> usp2;
usp2.reset(new int(88));
//初始化方式3
unique_ptr<int> usp3 = make_unique<int>(99);
//std::unique_ptr 拷贝构造函数和赋值运算符都被删除
//unique_ptr<int> usp4(make_unique<int>(99));
//unique_ptr<int> usp5(usp4);
//unique_ptr<int> usp6 = usp4;
//可以通过一个函数返回一个 std::unique_ptr
unique_ptr<int> usp7 = func(345);
cout << "*usp7: " << *usp7 << endl;
//使用移动构造
unique_ptr<int> usp8(make_unique<int>(99));
cout << "usp8: " << usp8 << endl;
unique_ptr<int> usp9(std::move(usp8));
cout << "usp8: " << usp8 << " usp9: " << usp9 << endl;
unique_ptr<int> usp10 = std::move(usp9);
cout << "usp8: " << usp8 << " usp9: " << usp9 << " usp10: "<< usp10 << endl;
//std::unique_ptr还可以持有一组堆对象
//创建10个int类型的堆对象
//形式1
unique_ptr<int[]> usp11(new int[10]);
//形式2
unique_ptr<int[]> usp12;
usp12.reset(new int[10]);
//形式3
unique_ptr<int[]> usp13 = make_unique<int[]>(10);
for (int i = 0; i < 10; ++i)
{
usp11[i] = i;
usp12[i] = i;
usp13[i] = i;
}
for (int i = 0; i < 10; ++i)
{
cout << usp11[i] << ", " << usp12[i] << ", " << usp13[i] << endl;
}
//-----------------------------------------------
shared_ptr<int> sp1 = make_shared<int>(42);
cout << "sp1: " << sp1 << " *sp1: " << *sp1
<< " sp1.get(): " << sp1.get() << endl;
}