forked from mcpp-community/d2mcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-move-semantics-0.cpp
More file actions
76 lines (62 loc) · 1.9 KB
/
05-move-semantics-0.cpp
File metadata and controls
76 lines (62 loc) · 1.9 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
// d2mcpp: https://github.com/mcpp-community/d2mcpp
// license: Apache-2.0
// file: dslings/cpp11/05-move-semantics-0.cpp
//
// Exercise/练习: cpp11 | 05 - move semantics | 移动构造与触发时机
//
// Tips/提示: 观察编译器输出, 在不改变buff传递的逻辑, 使得只做一次资源的分配和释放
//
// Docs/文档:
// - https://en.cppreference.com/w/cpp/language/move_constructor
//
// Auto-Checker/自动检测命令:
//
// d2x checker move-semantics
//
#include <d2x/cpp/common.hpp>
#include <iostream>
struct Buffer {
int *data;
Buffer() : data { new int[2] {0, 1} } {
std::cout << "Buffer():" << data << std::endl;
}
Buffer(const Buffer &other) {
std::cout << "Buffer(const Buffer&):" << data << std::endl;
data = new int[2];
data[0] = other.data[0];
data[1] = other.data[1];
}
Buffer(Buffer&& other) : data { other.data } {
std::cout << "Buffer(Buffer&&):" << data << std::endl;
other.data = nullptr; // 让原对象的指针失效
}
~Buffer() {
if (data) {
std::cout << "~Buffer():" << data << std::endl;
delete[] data;
}
}
const int * data_ptr() const { return data; }
};
Buffer process(Buffer buff) {
std::cout << "process(): " << buff.data << std::endl;
return buff;
}
int main() {
{
Buffer buff1 = process(Buffer());
auto buff1DataPtr = buff1.data_ptr();
std::cout << " --- " << std::endl;
Buffer buff2(std::move(buff1));
auto buff2DataPtr = buff2.data_ptr();
d2x_assert(buff1DataPtr == buff2DataPtr);
Buffer buff3 = buff2;
auto buff3DataPtr = buff3.data_ptr();
d2x_assert(buff2DataPtr == buff3DataPtr);
Buffer buff4 = process(buff3);
auto buff4DataPtr = buff4.data_ptr();
d2x_assert(buff3DataPtr == buff4DataPtr);
}
D2X_WAIT
return 0;
}