-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathro5
More file actions
91 lines (75 loc) · 3.75 KB
/
Copy pathro5
File metadata and controls
91 lines (75 loc) · 3.75 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
87
88
89
90
91
// Template for the Rule Of 5
class Ro5 {
private:
A classA;
B *classBptr;
C *malloc_ptr;
int private1;
std::container private2;
public:
// default constructor, happens in places where you make delarations without initialization. eg:
// Ro5 my_class;
// Now, if don't want invoking your default constructor without initialization, you need to
// replace the next line with: "Ro5() = delete;" If you do that, statements like
// Ro5 my_class; will throw compilation error, but statement like
// Ro5 my_class{arg1, arg2, ..., argk}; would not
Ro5() : classA({}), classBptr(new B{}), malloc_ptr(new C{}), private1(0), private2(std::container{}) {};
// parameterized constructor
Ro5(const A &anotherClassA, const B *anotherClassBptr, C *anotherMallocPtr, int i, std::container anotherContainer) :
classA(anotherClassA), // will invoke copy constructor of A
classBptr(new B(*anotherClassBptr)),
malloc_ptr(std::memcpy(std::malloc(sizeof(C)), anotherMallocPtr, sizeof(C))),
private1{i},
private2{anotherContainer} // std::container will automatically initialized with anotherContainer values
{};
~Ro5() {
del malloc_ptr;
del classBptr;
// remaining members are automatically freed
}
// Copy constructor
Ro5(const Ro5 &other) :
classA(other.classA), // will invoke copy constructor of A
classBptr(new B(*other.classBptr)),
malloc_ptr(std::memcpy(std::malloc(sizeof(C)), other.malloc_ptr, sizeof(C))),
private1{other.private1},
private2{other.private2} // std containers will auto-initialize
{};
// Move Constructor
Ro5(Ro5 &&other) :
classA(other.classA), // will invoke move constructor of A
classBptr{other.classBptr}; // copy the pointer to heap address
malloc_ptr{other.malloc_ptr}, // copy the pointer to heap address
private1{other.private1},
private2{other.private2}, // invoke move constructor of std container
{
other.classBptr = nullptr; // release the other heap allocated mem
other.malloc_ptr = nullptr; // release the other heap allocated mem
};
// Copy assignment Operator
Ro5 &operator=(const Ro5 &other) {
if (this != other) {
classA = other.classA; // will invoke copy assignment operator of A
delete classBptr; // always do for heap allocated mem
classBptr = new B(*other.classBptr); // use 'new' for class pointer
delete malloc_ptr; // always do for heap allocated mem
malloc_ptr = std::memcpy(std::malloc(sizeof(C)), other.malloc_ptr, sizeof(C));
private1 = other.private1;
private2 = other.private2; // will invoke copy assignment operator of standard container
}
return *this;
};
// Move assignment Operator
Ro5 &operator=(Ro5 &&other) {
if (this != other) {
classA = std::exchange(other.classA, std::nullptr);
delete classBptr; // delete previous heap mem allocation
classBptr = other.classBptr; // point it to new heap location
other.classBptr = std::nullptr; // release previous hold on this heap
delete malloc_ptr // delete previous heap mem allocation
malloc_ptr = other.malloc_ptr; // point it to new heap location
other.malloc_ptr = std::nullptr; // release previous hold on this heap
}
return *this;
};
};