-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_move_constructors.cpp
More file actions
39 lines (31 loc) · 1023 Bytes
/
copy_move_constructors.cpp
File metadata and controls
39 lines (31 loc) · 1023 Bytes
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
class MyResource {
public:
// Constructors and destructor
MyResource() : data(new int[100]) {}
~MyResource() { delete[] data; }
// Copy constructor
MyResource(const MyResource& other) : data(new int[100]) {
std::copy(other.data, other.data + 100, data);
}
// Copy assignment operator
MyResource& operator=(const MyResource& other) {
if (&other == this) { return *this; }
std::copy(other.data, other.data + 100, data);
return *this;
}
// Move constructor
// MyResource(MyResource&& other) noexcept : data(std::move(other.data)) {
MyResource(MyResource&& other) noexcept : data(other.data) {
other.data = nullptr;
}
// Move assignment operator
MyResource& operator=(MyResource&& other) noexcept {
if (&other == this) { return *this; }
delete[] data;
data = other.data;
other.data = nullptr;
return *this;
}
private:
int* data;
};