-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_test.cpp
More file actions
65 lines (52 loc) · 1.36 KB
/
Copy pathmove_test.cpp
File metadata and controls
65 lines (52 loc) · 1.36 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
#include <iostream>
#include <vector>
#include <string>
class MoveTest {
public:
MoveTest() {
}
MoveTest(const MoveTest& a) {
std::cout << " copy struct is using\n";
p = new char(*(a.p));
}
MoveTest(MoveTest&& a) {
std::cout << " move struct is using\n";
p = a.p;
a.p = nullptr;
delete a.p;
a.a = 100;
this->a = 99;
}
~MoveTest() {
if (p){
delete p;
}
}
void add_char(char text) {
p = new char(text);
}
void change_vec(int a) {
vec[0] = a;
}
int a;
char* p = nullptr;
std::vector<int> vec {4};
};
void test_f(MoveTest test) {
std::cout << " ===after move, test.a is " <<test.a <<" test.vec is " << test.vec.front() << std::endl;
test.a = 1;
}
void test_f(MoveTest& test) {
std::cout << " ===++after move, test.a is " << test.a <<" test.vec is " << test.vec.front() << std::endl;
test.a = 1;
}
int main() {
MoveTest test1;
test1.add_char('a');
MoveTest test2 = test1;
MoveTest test3 = std::move(test2);
test3.change_vec(9);
test_f(std::move(test3)); // after add std::move to test3, the move is using, which made the test3 undefined;
std::cout << " after move, test3.a is " << test3.a << " test3.vec is "<< test3.vec.front() << std::endl;
return 0;
}