-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
99 lines (86 loc) · 2.14 KB
/
main.cpp
File metadata and controls
99 lines (86 loc) · 2.14 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <string>
#include <iostream>
struct data
{
std::string str1 = "hello";
std::string str2 = "world";
};
class foo
{
public:
//default Constructor
foo()
{
std::cout<<"foo inside default constructor"<<std::endl;
}
//Constructor
explicit foo(data * dataIn)
{
address = dataIn;
std::cout<<"foo inside conversion constrctor"<<std::endl;
}
//destructor
~foo()
{
address = nullptr;
std::cout<<"foo inside destructor"<<std::endl;
}
//copy constructor
foo(const foo & copy)
{
address = copy.address; //shallow copy
std::cout<<"foo inside copy constructor"<<std::endl;
}
//assignment operator
foo& operator=(const foo & copy){
std::cout<<"foo inside assignment operator"<<std::endl;
//Check if doing deep copy plus other things
if(this != ©)
{
address = copy.address; //shallow copy
}
return *this;
}
//Move constructorfoo inside copy constructor
foo(foo && other){
std::cout<<"foo inside move constructor"<<std::endl;
address = other.address;
other.address = nullptr;
}
//move assignment operator
foo& operator=(foo && other )
{
std::cout<<"foo inside move assignment operator"<<std::endl;
//Check make sure that you do not null foo inside copy constructor your self out
if(this != &other)
{
address = other.address;
other.address = nullptr;
}
return *this;
}
private:
data * address;
};
foo createFoo()
{
// return std::move(foo());//calls default , move constructor
return foo();//calls only default constructor to do compiler opimiztion
}
int main()
{
data data1;
//foo, foo;
//foo myfoo0;
// foo myfoo(&data1);
//std::cout<<"Line"<<std::endl;
//foo myfoo2A(myfoo);
//std::cout<<"Line"<<std::endl;
//foo myfoo2B = myfoo;
//std::cout<<"Line"<<std::endl;
// foo myfoo3 = std::move(myfoo);
foo myfoo4 = createFoo();
std::cout<<"Line"<<std::endl;
//foo myfoo3 = foo(myfoo);
return 0;
}