-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart_pointers.cpp
More file actions
37 lines (27 loc) · 790 Bytes
/
Copy pathsmart_pointers.cpp
File metadata and controls
37 lines (27 loc) · 790 Bytes
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
/* This file contains dynamic memory support provided by the C++
Standard Library using smart pointers */
#include <iostream>
#include <memory>
using namespace std;
class Person{
string name;
int age;
public:
Person(string a, int b): name(a), age(b) {};
~Person()
{
cout << "Destructor invoked" << endl;
}
};
int main()
{
unique_ptr<Person>uPtr1 = make_unique<Person>("Adhi", 1);
// unique_ptr<Person>uPtr1 (new Person("Adhi", 1));
shared_ptr<Person>shPtr1 = make_shared<Person>("Adhithyan", 2);
cout << shPtr1.use_count() << endl;
shared_ptr<Person>shPtr2 = shPtr1;
cout << shPtr2.use_count() << endl;
weak_ptr<Person>wkPtr1 = shPtr2;
cout << shPtr2.use_count() << endl;
return 0;
}