-
Notifications
You must be signed in to change notification settings - Fork 0
/
play-with-uniqueptrs.cpp
62 lines (48 loc) · 961 Bytes
/
play-with-uniqueptrs.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
//playing with unique pointers
#include<iostream>
#include<conio.h>
#include<memory>
#include<cstring>
class A
{
public:
int m_num;
std::string m_mystring;
A(int num,std::string mystring):m_num(num),m_mystring(mystring)
{
}
};
auto makeA()->std::unique_ptr<A>
{
return std::unique_ptr<A> {new A(45,"vasant")};
};
auto updateA(std::unique_ptr<A>temp_ptr)->std::unique_ptr<A>
{
temp_ptr->m_num = 12;
temp_ptr->m_mystring="preeti";
return temp_ptr;
}
int main()
{
using namespace std;
//create a unique pointer and make it point to an object of this class
//auto ptr1=unique_ptr<A>(new A);
auto ptr = makeA();
if (ptr)
{
cout<<"ptr holds it !"<<endl;
}
auto ptr2=move(ptr);
if (ptr)
{
cout<<"ptr holds it !"<<endl;
}
if (ptr2)
{
cout<<"ptr2 holds it !"<<endl;
}
ptr2=updateA(move(ptr2));
cout<<ptr2->m_num<<" "<<ptr2->m_mystring<<endl;
getch();
return 0;
}