-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartPointers.cpp
127 lines (107 loc) · 3.57 KB
/
smartPointers.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
#include <iostream>
#include <memory>
using namespace std;
class MyClass
{
public:
MyClass()
{
std::cout << "Constructir invoked." << endl;
}
~MyClass()
{
std::cout << "Destructor invoked." << endl;
}
};
int main()
{
// 1. UNIQUE POINTER
// unique_ptr<int> uniq_pointer1 = make_unique<int>(25);
// unique_ptr<int> uniq_pointer2 = move(uniq_pointer1);
// cout << uniq_pointer1 << endl;
// cout << *uniq_pointer1 << endl;
// cout << *uniq_pointer2 << endl;
// MyClass obj = MyClass();
// {
// unique_ptr<MyClass> pointer = make_unique<MyClass>();
// }
// cout << pointer << endl;
// 2. SHARED POINTER
// shared_ptr<int> shPointer1 = make_shared<int>(25);
// cout << "# of owners of shPointer1: " << shPointer1.use_count() << endl;
// shared_ptr<int> pointer2 = shPointer1;
// cout << "# of owners of shPointer1: " << shPointer1.use_count() << endl;
// cout << "# of owners of pointer2: " << pointer2.use_count() << endl;
// shared_ptr<int> pointer3 = move(shPointer1);
// cout << "# of owners of shPointer1: " << shPointer1.use_count() << endl;
// cout << "# of owners of pointer3: " << pointer2.use_count() << endl;
// shared_ptr<int> pointer_1 = make_shared<int>(41);
// cout << "No of. owners: " << pointer_1.use_count() << endl;
// {
// shared_ptr<int> pointer_2 = pointer_1;
// cout << "No of. owners: " << pointer_1.use_count() << endl;
// }
// cout << "No of. owners: " << pointer_1.use_count() << endl;
// 3. WEAK POINTER
// weak_ptr<int> wpointer1;
// unique_ptr<int> uPointer = make_unique<int>(100);
// // wpointer1 = uPointer;
// {
// // shared_ptr<int> shpointer1 = make_shared<int>(25);
// auto shpointer1 = make_shared<int>(25);
// wpointer1 = shpointer1;
// }
// // cout << "Value: " << *shpointer1 << endl;
// std::cout << wpointer1.expired() << endl;
// std::cout << wpointer1.lock() << endl; // returns a temporary shared pointer
weak_ptr<int> wPointer;
auto shPointer1 = make_shared<int>(1200);
auto shPointer2 = shPointer1;
auto shPointer3 = shPointer1;
auto shPointer4 = shPointer1;
wPointer = shPointer1;
std::cout << "No. of owners: " << shPointer1.use_count() << endl;
std::cout << "No. of owners: " << shPointer2.use_count() << endl;
std::cout << "No. of owners: " << shPointer3.use_count() << endl;
std::cout << "No. of owners: " << shPointer4.use_count() << endl;
shPointer1.reset();
if (auto tempPointer = wPointer.lock())
{
std::cout << "The value: " << *tempPointer << endl;
}
else
{
std::cout << "No objects.";
}
shPointer2.reset();
if (auto tempPointer = wPointer.lock())
{
std::cout << "The value: " << *tempPointer << endl;
}
else
{
std::cout << "No objects.";
}
shPointer3.reset(new int(10));
if (auto tempPointer = wPointer.lock())
{
std::cout << "The value: " << *tempPointer << endl;
}
else
{
std::cout << "No objects.";
}
shPointer4.reset();
if (auto tempPointer = wPointer.lock())
{
std::cout << "The value: " << *tempPointer << endl;
}
else
{
std::cout << "No objects." << endl;
}
std::cout << "No. of owners: " << shPointer1.use_count() << endl;
std::cout << "No. of owners: " << shPointer2.use_count() << endl;
std::cout << "No. of owners: " << shPointer3.use_count() << endl;
std::cout << "No. of owners: " << shPointer4.use_count() << endl;
}