-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp2_6.html
More file actions
86 lines (76 loc) · 3.04 KB
/
Copy pathCpp2_6.html
File metadata and controls
86 lines (76 loc) · 3.04 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
85
86
<article>
<h1>C++ II: Page 6 - Modern C++: Move Semantics</h1>
<section>
<h2>Optimizing Performance</h2>
<p>Move semantics allow C++ to "transfer" the ownership of resources from one object to another instead of performing expensive copies. This is achieved using rvalue references (<code>&&</code>).</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: move_semantics.cpp
* Demonstration of Move Constructor and Move Assignment (Rule of Five)
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Buffer {
size_t size;
int* data;
public:
Buffer(size_t s) : size(s), data(new int[s]) {}
~Buffer() { delete[] data; }
// 1. Move constructor
Buffer(Buffer&& other) noexcept : size(other.size), data(other.data) {
other.size = 0;
other.data = nullptr;
cout << "Buffer moved." << endl;
}
// 2. Move assignment operator
Buffer& operator=(Buffer&& other) noexcept {
if (this != &other) {
delete[] data;
size = other.size;
data = other.data;
other.size = 0;
other.data = nullptr;
cout << "Buffer move-assigned." << endl;
}
return *this;
}
};
int main() {
Buffer b1(1000);
// Use std::move to transfer ownership
Buffer b2 = move(b1);
Buffer b3(500);
b3 = move(b2);
return 0;
}
/*
* Move Semantics Nuances:
* 1. '&&' indicates an rvalue reference (temporary object).
* 2. Move constructor takes '&&', steals resources, sets old data to null.
* 3. std::move() casts an object to an rvalue, enabling a move.
* 4. Exception safety (noexcept) is required for move constructors.
* 5. Move semantics drastically improve performance for container classes.
* 6. Rule of Five: If you implement a destructor, you should consider
* implementing copy constructor, copy assignment, move constructor,
* and move assignment to maintain resource integrity.
*/
</code></pre>
<div class="interactive-sim">
<h3>Move Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp2_6');
out.innerHTML = 'Buffer b2 = move(b1);<br>';
setTimeout(() => out.innerHTML += '>> Resources stolen from b1, b2 owns data!', 800);
">Run Simulation</button>
<div id="sim-output-cpp2_6" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp2_5.html" style="text-decoration: none; color: #6c757d;">← Previous: Modern C++: Smart Pointers</a>
<a href="#" data-file="Cpp2_7.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Modern C++: Auto & Lambdas →</a>
</div>
</footer>
</article>