-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice5.cpp
55 lines (40 loc) · 1.95 KB
/
practice5.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
#include <iostream>
#include <memory>
using namespace std;
int main()
{
int a = 258497;
auto sh_pointer_1 = make_shared<int>(a);
cout << "Number of owners: " << sh_pointer_1.use_count() << endl;
auto sh_pointer_2 = sh_pointer_1;
cout << "Number of owners: " << sh_pointer_1.use_count() << endl;
cout << "Number of owners: " << sh_pointer_2.use_count() << endl;
cout << "Owner before: " << sh_pointer_1.owner_before(sh_pointer_2) << endl;
cout << "sh_pointer_1 :" << *sh_pointer_1 << endl;
cout << "sh_pointer_2 :" << *sh_pointer_2 << endl;
auto sh_pointer_3 = sh_pointer_1;
auto sh_pointer_4 = sh_pointer_1;
auto sh_pointer_5 = sh_pointer_1;
cout << "Number of owners: " << sh_pointer_1.use_count() << endl;
cout << "Number of owners: " << sh_pointer_2.use_count() << endl;
cout << "Number of owners: " << sh_pointer_3.use_count() << endl;
cout << "Number of owners: " << sh_pointer_4.use_count() << endl;
cout << "Number of owners: " << sh_pointer_5.use_count() << endl;
cout << "----------------------------" << endl;
sh_pointer_1.reset();
cout << "Number of owners: " << sh_pointer_1.use_count() << endl;
cout << "Number of owners: " << sh_pointer_2.use_count() << endl;
cout << "Number of owners: " << sh_pointer_3.use_count() << endl;
cout << "Number of owners: " << sh_pointer_4.use_count() << endl;
cout << "Number of owners: " << sh_pointer_5.use_count() << endl;
cout << "----------------------------" << endl;
sh_pointer_1.reset(); // does nothing
sh_pointer_3.reset();
sh_pointer_4.reset();
cout << "Number of owners: " << sh_pointer_1.use_count() << endl;
cout << "Number of owners: " << sh_pointer_2.use_count() << endl;
cout << "Number of owners: " << sh_pointer_3.use_count() << endl;
cout << "Number of owners: " << sh_pointer_4.use_count() << endl;
cout << "Number of owners: " << sh_pointer_5.use_count() << endl;
return 1;
}