-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.11 MoveConstructorAssignmentOperator.cpp
More file actions
149 lines (127 loc) · 3.53 KB
/
12.11 MoveConstructorAssignmentOperator.cpp
File metadata and controls
149 lines (127 loc) · 3.53 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <string.h>
using namespace std;
class MyString
{
private:
char* buffer;
MyString(): buffer(NULL) // private default constructor
{
cout << "Default constructor called" << endl;
}
public:
MyString(const char* initialInput) // constructor
{
cout << "Constructor called for: " << initialInput << endl;
if(initialInput != NULL)
{
buffer = new char [strlen(initialInput) + 1];
strcpy(buffer, initialInput);
}
else
buffer = NULL;
}
MyString(MyString&& moveSrc) // move constructor
{
cout << "Move constructor moves: " << moveSrc.buffer << endl;
if(moveSrc.buffer != NULL)
{
buffer = moveSrc.buffer; // take ownership i.e. 'move'
moveSrc.buffer = NULL; // free move source
}
}
MyString& operator= (MyString&& moveSrc) // move assignment op.
{
cout << "Move assignment op. moves: " << moveSrc.buffer << endl;
if((moveSrc.buffer != NULL) && (this != &moveSrc))
{
delete[] buffer; // release own buffer
buffer = moveSrc.buffer; // take ownership i.e. 'move'
moveSrc.buffer = NULL; // free move source
}
return *this;
}
MyString(const MyString& copySrc) // copy constructor
{
cout << "Copy constructor copies: " << copySrc.buffer << endl;
if (copySrc.buffer != NULL)
{
buffer = new char[strlen(copySrc.buffer) + 1];
strcpy(buffer, copySrc.buffer);
}
else
buffer = NULL;
}
MyString& operator= (const MyString& copySrc) // Copy assignment op.
{
cout << "Copy assignment op. copies: " << copySrc.buffer << endl;
if ((this != ©Src) && (copySrc.buffer != NULL))
{
if (buffer != NULL)
delete[] buffer;
buffer = new char[strlen(copySrc.buffer) + 1];
strcpy(buffer, copySrc.buffer);
}
return *this;
}
~MyString() // destructor
{
if (buffer != NULL)
delete[] buffer;
}
int GetLength()
{
return strlen(buffer);
}
operator const char*()
{
return buffer;
}
MyString operator+ (const MyString& addThis)
{
cout << "operator+ called: " << endl;
MyString newStr;
if (addThis.buffer != NULL)
{
newStr.buffer = new char[GetLength()+strlen(addThis.buffer)+1];
strcpy(newStr.buffer, buffer);
strcat(newStr.buffer, addThis.buffer);
}
return newStr;
}
};
int main()
{
MyString Hello("Hello ");
MyString World("World");
MyString CPP(" of C++");
MyString sayHelloAgain ("overwrite this");
sayHelloAgain = Hello + World + CPP;
return 0;
}
/*
Without move constructor and move assignment operator:
Constructor called for: Hello
Constructor called for: World
Constructor called for: of C++
Constructor called for: overwrite this
operator+ called:
Default constructor called
Copy constructor to copy from: Hello World
operator+ called:
Default constructor called
Copy constructor to copy from: Hello World of C++
Copy assignment operator to copy from: Hello World of C++
With move constructor and move assignment operators:
Constructor called for: Hello
Constructor called for: World
Constructor called for: of C++
Constructor called for: overwrite this
operator+ called:
Default constructor called
Move constructor to move from: Hello World
operator+ called:
Default constructor called
Move constructor to move from: Hello World of C++
Move assignment operator to move from: Hello World of C++
*/