forked from learncppnow/9E
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.12 MoveConstructorAssignmentOperator.cpp
121 lines (97 loc) · 2.83 KB
/
12.12 MoveConstructorAssignmentOperator.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <algorithm>
using namespace std;
class MyBuffer
{
private:
int* myNums;
unsigned int bufLength;
public:
MyBuffer(unsigned int length)
{
cout << "Constructing new instance with " \
<< length << " elements" << endl;
bufLength = length;
myNums = new int[length]; // allocate memory
}
MyBuffer(const MyBuffer& src) // copy constructor
{
cout << "Copy constructor creating deep copy" << endl;
bufLength = src.bufLength;
myNums = new int[bufLength];
copy(src.myNums, src.myNums + bufLength, myNums); // deep copy
}
MyBuffer(MyBuffer&& src) // move constructor
{
cout << "Move constructor transferring ownership" << endl;
if (src.myNums != NULL)
{
bufLength = src.bufLength;
myNums = src.myNums; // take ownership
src.myNums = NULL;
src.bufLength = 0;
}
}
MyBuffer& operator= (const MyBuffer& src) // copy assignment
{
cout << "Copy Assignment creating deep copy" << endl;
if (myNums != src.myNums) // avoid copy to self
{
if (myNums)
delete myNums;
bufLength = src.bufLength;
myNums = new int[bufLength];
copy(src.myNums, src.myNums + bufLength, myNums); // deep copy
}
return *this;
}
MyBuffer& operator= (MyBuffer&& src) // move assignment
{
cout << "Move assignment transferring ownership" << endl;
if ((src.myNums != NULL) && (myNums != src.myNums))
{
delete[] myNums;
myNums = src.myNums; // take ownership
bufLength = src.bufLength;
src.bufLength = 0;
src.myNums = NULL;
}
return *this;
}
MyBuffer operator + (const MyBuffer& bufToAppend)
{
cout << "Operator + concatenating" << endl;
MyBuffer newBuf(this->bufLength + bufToAppend.bufLength);
for (unsigned int counter = 0; counter < bufLength; ++counter)
newBuf.SetValue(counter, *(myNums + counter));
for (unsigned int counter = 0; counter < bufToAppend.bufLength; ++counter)
newBuf.SetValue(counter + bufLength, *(bufToAppend.myNums + counter));
return newBuf;
}
~MyBuffer()
{
delete[] myNums; // free allocated memory
}
void SetValue(unsigned int index, int value)
{
if (index < bufLength) // check for bounds
*(myNums + index) = value;
}
void DisplayBuf()
{
for (unsigned int counter = 0; counter < bufLength; ++counter)
cout << *(myNums + counter) << " ";
cout << endl;
}
};
int main()
{
MyBuffer buf1(5);
MyBuffer buf2(15);
cout << "Concatenation at object instantiation: " << endl;
MyBuffer buf3(buf1 + buf2);
MyBuffer bufSum(1);
cout << "Concatenation at assignment: " << endl;
bufSum = buf1 + buf2 + buf3;
return 0;
}