forked from coders-school/modern-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.cpp
More file actions
48 lines (46 loc) · 967 Bytes
/
Copy pathrules.cpp
File metadata and controls
48 lines (46 loc) · 967 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
40
41
42
43
44
45
46
47
48
#include <utility>
#include <iostream>
template <typename T>
class X {
T* ptr {};
int a = 4;
int b = 10;
public:
X(T* p) : ptr(p) {}
~X() {
delete ptr;
}
X(const X & other) : ptr(new T{*other.ptr}) {}
X& operator=(const X & other) {
if (&other != this) {
delete ptr;
ptr = new T{*other.ptr};
}
return *this;
}
X(X && other) : ptr(other.ptr) {
other.ptr = nullptr;
}
X& operator=(X && other) {
if (&other != this) {
delete ptr;
ptr = other.ptr;
other.ptr = nullptr;
}
return *this;
}
T& get() {
return *ptr;
}
};
int main() {
X<int> x{new int{42}};
x = x;
X<int> x2{new int{42}};
X<int> x3 = x; // copy constructor
x2 = x; // copy assignment
X<int> x4{std::move(x2)};
x3 = std::move(x);
std::cout << x4.get();
return 0;
}