-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathtest.cpp
More file actions
76 lines (64 loc) · 1.26 KB
/
test.cpp
File metadata and controls
76 lines (64 loc) · 1.26 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
#include <functional>
#include <string>
class A {
public:
A(const A &oth) {}
A(const A &&oth) noexcept {}
A &operator=(const A &oth) { // compliant
A tmp(oth);
Swap(*this, tmp);
return *this;
}
A &operator=(A &&oth) noexcept { // compliant
A tmp(std::move(oth));
Swap(*this, tmp);
return *this;
}
static void Swap(A &lhs, A &rhs) noexcept {
std::swap(lhs.ptr1, rhs.ptr1);
std::swap(lhs.ptr2, rhs.ptr2);
}
private:
int *ptr1;
int *ptr2;
};
class B {
public:
B &operator=(const B &oth) // COMPLIANT
{
if (this != &oth) {
int *tmpPtr = new int(*(oth.nPtr));
delete nPtr;
nPtr = tmpPtr;
}
return *this;
}
B &operator=(B &&oth) noexcept { // COMPLIANT
if (this != &oth) {
int *tempPtr = new int(*(std::move(oth.nPtr)));
delete nPtr;
nPtr = tempPtr;
}
return *this;
}
private:
int *nPtr = nullptr;
};
class C {
public:
C &operator=(const C &oth) // NON_COMPLIANT
{
int *tmpPtr = new int(*(oth.nPtr));
delete nPtr;
nPtr = tmpPtr;
return *this;
}
C &operator=(C &&oth) noexcept { // NON_COMPLIANT
int *tempPtr = new int(*(std::move(oth.nPtr)));
delete nPtr;
nPtr = tempPtr;
return *this;
}
private:
int *nPtr = nullptr;
};