-
Notifications
You must be signed in to change notification settings - Fork 14
/
move.cpp
57 lines (42 loc) · 1.04 KB
/
move.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
#include <iostream>
#include <string>
using namespace std;
class Fruit {
public:
Fruit(string& label_) : label(label_) { }
Fruit(string&& label_) : label(move(label_)) { }
/* copy constructor*/
Fruit(const Fruit& other) : label(other.label) {
cout << "copy constructor" << endl;
}
/* copy assign */
Fruit& operator=(Fruit& other) {
cout << "copy assign" << endl;
label = other.label;
return *this;
}
/* move constructor */
Fruit(Fruit&& other) : label(move(other.label)) {
cout << "move constructor" << endl;
}
/* move assign */
Fruit& operator=(Fruit&& other) {
cout << "move assign" << endl;
label = move(other.label);
return *this;
}
string getLabel() { return label; }
private:
string label;
};
int main() {
Fruit firstFruit("extra juicy");
/* copy */
Fruit secondFruit(firstFruit);
/* move */
Fruit thirdFruit(move(firstFruit));
/* copy */
secondFruit = thirdFruit;
/* move */
firstFruit = move(thirdFruit);
}