Skip to content

Commit 12279ec

Browse files
committed
-> weak_pointers and its methods.
1 parent 4fdf7d0 commit 12279ec

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

notes_3_smart_pointers.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ int main()
4747
// Meaning, only one 'unique pointer' can point to a
4848
// variable's address. If we try to assign a different
4949
// pointer to point the same variable. It gives error.
50+
// We can move the ownership of one unique-pointer to another,
51+
// using the move() method. However, the first pointer will be
52+
// deallocated.
5053

51-
// SHARED POINERS.
54+
// SHARED POINTERS.
5255
// Shared pointers, are the smart pointer that are basically
5356
// opposite of the unique pointers. Meaning, multiple shared
5457
// pointers can point towards the same memory address.

notes_4_smart_pointers_practice.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <iostream>
2+
#include <memory>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
auto un_pointer_1 = make_unique<int>(516);
9+
10+
cout << "un_pointer_1: " << un_pointer_1.get() << endl;
11+
cout << "*un_pointer_1: " << *un_pointer_1 << endl;
12+
13+
// auto un_pointer_2 = un_pointer_1;
14+
auto un_pointer_2 = move(un_pointer_1);
15+
16+
cout << "un_pointer_1: " << un_pointer_1.get() << endl;
17+
// cout << "*un_pointer_1: " << *un_pointer_1 << endl;
18+
19+
cout << "un_pointer_2: " << un_pointer_2.get() << endl;
20+
cout << "*un_pointer_2: " << *un_pointer_2 << endl;
21+
22+
shared_ptr<int> sh_pointer_1 = make_shared<int>(325789);
23+
24+
cout << "sh_pointer_1: " << sh_pointer_1.get() << endl;
25+
cout << "*sh_pointer_1: " << *sh_pointer_1 << endl;
26+
27+
cout << "sh_pointer_1.unique(): " << sh_pointer_1.unique() << endl; // true
28+
29+
// WOW!! Until a shared_pointer has one owner we can move the,
30+
// ownership of unique pointer to the shared pointer.
31+
sh_pointer_1 = move(un_pointer_2);
32+
33+
auto sh_pointer_2 = sh_pointer_1;
34+
35+
cout << "sh_pointer_2: " << sh_pointer_2.get() << endl;
36+
cout << "*sh_pointer_2: " << *sh_pointer_2 << endl;
37+
38+
cout << "sh_pointer_1.use_count(): " << sh_pointer_1.use_count() << endl;
39+
cout << "sh_pointer_1.unique(): " << sh_pointer_1.unique() << endl; // false
40+
41+
cout << endl
42+
<< endl;
43+
44+
weak_ptr<int> wk_pointer = sh_pointer_1;
45+
46+
cout << "wk_pointer.expired(): " << wk_pointer.expired() << endl; // false
47+
cout << "wk_pointer.use_count(): " << wk_pointer.use_count() << endl; // 2
48+
49+
sh_pointer_1.reset();
50+
cout << "wk_pointer.expired(): " << wk_pointer.expired() << endl; // false
51+
cout << "wk_pointer.use_count(): " << wk_pointer.use_count() << endl; // 1
52+
53+
sh_pointer_2.reset();
54+
cout << "wk_pointer.expired(): " << wk_pointer.expired() << endl; // true
55+
cout << "wk_pointer.use_count(): " << wk_pointer.use_count() << endl; // 0
56+
57+
// shared_pointer.expired() -> provides status of that shared pointer.
58+
// weak_pointer.expired() -> provides status of that all the shared pointer.
59+
60+
return 1;
61+
}

0 commit comments

Comments
 (0)