-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathr_value_move_syntax.cpp
More file actions
134 lines (112 loc) · 2.56 KB
/
r_value_move_syntax.cpp
File metadata and controls
134 lines (112 loc) · 2.56 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <cstring>
#include <iostream>
using namespace std;
class MyClass {
private:
int *heap_resource_handle_;
int size_;
public:
MyClass() {
heap_resource_handle_ = nullptr;
size_ = 0;
}
MyClass(int size) {
cout << "default constructor"
<< ", acquire resource " << endl;
heap_resource_handle_ = new int[size];
memset(heap_resource_handle_, 0, size);
size_ = size;
}
MyClass(const MyClass &object) {
cout << "copy constructor" << endl;
heap_resource_handle_ = new int[object.size_];
size_ = object.size_;
memcpy(heap_resource_handle_, object.heap_resource_handle_,
sizeof(int) * object.size_);
}
MyClass(MyClass &&object) {
cout << "move constructor" << endl;
heap_resource_handle_ = object.heap_resource_handle_;
object.heap_resource_handle_ = nullptr;
size_ = object.size_;
object.size_ = 0;
}
void release_resource() {
if (heap_resource_handle_ != nullptr) {
delete heap_resource_handle_;
cout << "release resource, size:" << size_ << endl;
}
}
MyClass &operator=(const MyClass &object) {
release_resource();
cout << "copy assignment" << endl;
heap_resource_handle_ = new int[object.size_];
size_ = object.size_;
memcpy(heap_resource_handle_, object.heap_resource_handle_,
sizeof(int) * object.size_);
return *this;
}
MyClass &operator=(MyClass &&object) {
release_resource();
cout << "move assignment" << endl;
heap_resource_handle_ = object.heap_resource_handle_;
size_ = object.size_;
object.heap_resource_handle_ = nullptr;
object.size_ = 0;
return *this;
}
~MyClass() {
cout << "destructor" << endl;
release_resource();
}
};
MyClass return_by_value() {
MyClass object(111);
return object;
}
void case00() {
cout << "\ncase00" << endl;
auto a = MyClass();
a = return_by_value();
}
void case0() {
cout << "\ncase0" << endl;
auto a = return_by_value();
}
void case1() {
cout << "\ncase1" << endl;
auto a = MyClass(10);
}
void case2() {
cout << "\ncase2" << endl;
auto b = MyClass(20);
auto a = b;
}
void case3() {
cout << "\ncase3" << endl;
auto c = MyClass(30);
auto d = move(c);
}
void case4() {
cout << "\ncase4" << endl;
auto b = MyClass(4);
auto a = MyClass(40);
a = b;
}
void case5() {
cout << "\ncase5" << endl;
auto c = MyClass(5);
auto d = MyClass(50);
d = move(c);
}
int main(int argc, char const *argv[]) {
case0();
case00();
case1();
case2();
case3();
// assignment constructor
case4();
case5();
return 0;
}