11// 02_move_semantics.cpp
2- // 移动构造与移动赋值:Buffer 资源所有权转移与 vector 操作对比
2+ // 移动构造与移动赋值:Buffer 资源转移全过程追踪
3+ // Standard: C++17
34
45#include < cstring>
56#include < iostream>
@@ -17,24 +18,40 @@ class Buffer {
1718 : data_(new char [capacity]), size_(0 ), capacity_(capacity) {
1819 std::cout << " [Buffer] 分配 " << capacity << " 字节\n " ;
1920 }
21+
2022 ~Buffer () {
2123 if (data_) {
2224 std::cout << " [Buffer] 释放 " << capacity_ << " 字节\n " ;
2325 delete[] data_;
2426 }
2527 }
28+
2629 Buffer (const Buffer& other)
2730 : data_(new char [other.capacity_]), size_(other.size_), capacity_(other.capacity_) {
2831 std::memcpy (data_, other.data_ , size_);
2932 std::cout << " [Buffer] 拷贝构造 " << capacity_ << " 字节\n " ;
3033 }
34+
3135 Buffer (Buffer&& other) noexcept
3236 : data_(other.data_), size_(other.size_), capacity_(other.capacity_) {
3337 other.data_ = nullptr ;
3438 other.size_ = 0 ;
3539 other.capacity_ = 0 ;
3640 std::cout << " [Buffer] 移动构造(指针转移)\n " ;
3741 }
42+
43+ Buffer& operator =(const Buffer& other) {
44+ if (this != &other) {
45+ delete[] data_;
46+ data_ = new char [other.capacity_ ];
47+ size_ = other.size_ ;
48+ capacity_ = other.capacity_ ;
49+ std::memcpy (data_, other.data_ , size_);
50+ std::cout << " [Buffer] 拷贝赋值 " << capacity_ << " 字节\n " ;
51+ }
52+ return *this ;
53+ }
54+
3855 Buffer& operator =(Buffer&& other) noexcept {
3956 if (this != &other) {
4057 delete[] data_;
@@ -48,17 +65,20 @@ class Buffer {
4865 }
4966 return *this ;
5067 }
68+
5169 void append (const char * str, std::size_t len) {
5270 if (size_ + len <= capacity_) {
5371 std::memcpy (data_ + size_, str, len);
5472 size_ += len;
5573 }
5674 }
75+
5776 std::size_t size () const { return size_; }
77+ std::size_t capacity () const { return capacity_; }
5878};
5979
6080int main () {
61- std::cout << " === 1. 创建缓冲区 ===\n " ;
81+ std::cout << " === 1. 创建两个缓冲区 ===\n " ;
6282 Buffer a (1024 );
6383 a.append (" Hello" , 5 );
6484 Buffer b (2048 );
@@ -71,17 +91,25 @@ int main() {
7191
7292 std::cout << " === 3. 移动构造 ===\n " ;
7393 Buffer d = std::move (b);
74- std::cout << " d.size() = " << d.size () << " \n\n " ;
94+ std::cout << " d.size() = " << d.size () << " \n " ;
95+ std::cout << " b.capacity() = " << b.capacity () << " \n\n " ;
7596
76- std::cout << " === 4. vector 中的移动 ===\n " ;
97+ std::cout << " === 4. 移动赋值 ===\n " ;
98+ a = std::move (d);
99+ std::cout << " a.size() = " << a.size () << " \n " ;
100+ std::cout << " d.capacity() = " << d.capacity () << " \n\n " ;
101+
102+ std::cout << " === 5. vector 中的移动 ===\n " ;
77103 std::vector<Buffer> buffers;
78104 buffers.reserve (4 );
79105 std::cout << " push_back 左值:\n " ;
80- buffers.push_back (c);
106+ buffers.push_back (c); // 拷贝
81107 std::cout << " push_back std::move:\n " ;
82- buffers.push_back (std::move (c));
108+ buffers.push_back (std::move (c)); // 移动
83109 std::cout << " emplace_back 原位构造:\n " ;
84- buffers.emplace_back (512 );
110+ buffers.emplace_back (512 ); // 直接在 vector 中构造
111+ std::cout << ' \n ' ;
85112
113+ std::cout << " === 6. 程序结束 ===\n " ;
86114 return 0 ;
87115}
0 commit comments