forked from mcpp-community/d2mcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-move-semantics-2.cpp
More file actions
84 lines (73 loc) · 2.36 KB
/
05-move-semantics-2.cpp
File metadata and controls
84 lines (73 loc) · 2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
// license: Apache-2.0
// file: dslings/cpp11/05-move-semantics-2.cpp
//
// Exercise/练习: cpp11 | 05 - move semantics | 移动的是资源而不是对象
//
// Tips/提示: 观察编译器输出, 使用移动语义并观察对比对象和资源的移动
//
// Docs/文档:
// - https://en.cppreference.com/w/cpp/language/move_constructor
//
// Auto-Checker/自动检测命令:
//
// d2x checker move-semantics-2
//
#include <d2x/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 & operator=(const Buffer &other) {
std::cout << "Buffer& operator=(const Buffer&):" << data << std::endl;
if (this != &other) {
delete[] data; // 释放旧资源
data = new int[2];
data[0] = other.data[0];
data[1] = other.data[1];
}
return *this;
}
Buffer & operator=(Buffer&& other) {
std::cout << "Buffer& operator=(Buffer&&):" << data << std::endl;
if (this != &other) {
delete[] data; // 释放旧资源 - 步骤1
data = other.data; // 转移资源 - 步骤2
other.data = nullptr; // 让原对象的指针失效 - 步骤3
}
return *this;
}
~Buffer() {
if (data) {
std::cout << "~Buffer():" << data << std::endl;
delete[] data;
}
}
const int * data_ptr() const {
return data;
}
};
int main() { // 移动语义 - 移动的是资源而不是对象演示
{
Buffer b1; // 调用默认构造函数
auto old_b1_data_ptr = b1.data_ptr();
Buffer b2 = b1; // std::move(b1);
d2x_assert(&b1 != &b2); // b1 和 b2 是不同的对象
d2x_assert(old_b1_data_ptr == b2.data_ptr());
d2x_assert(b1.data_ptr() == nullptr); // b1 的资源被移动了
}
D2X_WAIT
return 0;
}