-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01smart_pointers.cpp
49 lines (45 loc) · 1.5 KB
/
01smart_pointers.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
#include<iostream>
#include<memory>
using namespace std;
/*
wrapper around raw pointer and they deallocate memory automatically
types - unique, shared, weak
*/
class Pointer{
public:
Pointer(){
cout<<"Constructor invoked"<<endl;
}
~Pointer(){
cout<<"Destructor invoked"<<endl;
}
};
int main(){
// Unique Pointer
// unique_ptr<int> unptr1 = make_unique<int>(25);
// cout<<*unptr1<<endl;
// unique_ptr<int> unptr2 = move(unptr1);
// //unique pointer -> second pointer cannot store address to first pointer
// // instead we can move ownership and previous owner becomes a nullptr
// cout<<*unptr2<<endl;
// {
// unique_ptr<Pointer> unptr = make_unique<Pointer> ();
// }
//shared pointer can be shared between different owners , it has count of all owners
// shared_ptr<Pointer> shptr1 = make_shared<Pointer> ();
// shared_ptr<Pointer> shptr2 = shptr1;
// {// share pointer inside this scope will be deallocated after this scope ends
// shared_ptr<Pointer> shptr3 = shptr1;
// cout<<"Shared count: "<<shptr1.use_count()<<endl;
// }
// cout<<"Shared count: "<<shptr1.use_count()<<endl;
//Weak pointer does not increase use_count on sharing ownership
weak_ptr<int> wptr1;
{
shared_ptr<int> shptr1 = make_shared<int> (25);
wptr1 = shptr1;
//the memory of share_ptr was destroyed at end of this scope as
//weak_ptr doesnot keep its alive
}
system("pause>null");
}