-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyConstructorExample.cpp
54 lines (46 loc) · 1.41 KB
/
CopyConstructorExample.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
#include <bits/stdc++.h>
using namespace std;
/*
buffer: a region of a physical memory storage used to temporarily
store data while it is being moved from one place to another
*/
class String {
char* Buffer;
// unsigned is because size of the string cannot be negative
unsigned int Size;
public:
String(const char* str) {
Size = strlen(str);
Buffer = new char[Size + 1];
memcpy(Buffer, str, Size + 1);
Buffer[Size] = 0;
}
// copy constructor (declare what to do when someone try to copy the object of this class)
String(const String& other): Size(other.Size) {
// allocate a new buffer
Buffer = new char[Size + 1];
// copy the other buffer to Buffer
memcpy(Buffer, other.Buffer, Size + 1);
}
char& operator[](unsigned int index) {
return Buffer[index];
}
unsigned int size() {
return Size;
}
~String() {
delete[] Buffer;
}
friend ostream& operator<< (ostream &stream, const String& string);
friend String operator+(const String& firstString, const String& secondString);
};
ostream& operator<<(ostream &stream, const String& string) {
stream << string.Buffer;
return stream;
}
// Always pass the object by reference to avoid calling copy constructor
// The default copy constructor by C++ just copy the variables as it is.
int main() {
String s = "Ashu";
cout << s;
}