-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (63 loc) · 1.38 KB
/
main.cpp
File metadata and controls
81 lines (63 loc) · 1.38 KB
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
#include <iostream>
#include <memory>
using namespace std;
void foo(char ** mychar)
{
*mychar = new char{'a'};
}
std::unique_ptr<char> modernFoo()
{
//same as make_unique<char>('a');
//return std::unique_ptr<char> (new char{'a'});
return make_unique<char>('a');
}
bool modernFooWithBool(std::shared_ptr<char> & myvar)
{
try{
myvar = make_shared<char>('b');
}
catch( ...)
{
return false;
}
return true;
}
bool modernUniqueFooWithBool(std::unique_ptr<char> & myvar)
{
try{
myvar = make_unique<char>('c');
}
catch( ...)
{
return false;
}
return true;
}
bool modernUniqueMultiFooWithBool(std::unique_ptr<char> & myvar,std::unique_ptr<char> & myvar2)
{
try{
myvar = make_unique<char>('d');
myvar2 = make_unique<char>('e');
}
catch( ...)
{
return false;
}
return true;
}
int main(int argc, char *argv[])
{
char * t = nullptr;
foo(&t);
std::shared_ptr<char> sharemT ;
modernFooWithBool(sharemT);
std::unique_ptr<char> uP;
modernUniqueFooWithBool(uP);
auto mT = modernFoo();
std::unique_ptr<char> uP1;
std::unique_ptr<char> uP2;
modernUniqueMultiFooWithBool(uP1,uP2);
cout << "value of pointers: " <<*mT<<" "<< *sharemT<<" "<<*uP<<" "<<*uP1<<" "<<*uP2<<endl;
delete t;
return 0;
}