-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy_constructor_shallow_and_deep_copying.cpp
180 lines (148 loc) · 4.54 KB
/
Copy_constructor_shallow_and_deep_copying.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
using namespace std;
namespace example1{
class Wall {
public:
int length;
int* length2;
Wall() {
length = 10;
}
Wall(const Wall& w) { // shallow copy
length = w.length;
cout << "Copy constructor called: " << endl;
cout << length << endl;
length = 5;
cout << length;
}
Wall(int l) {
length2 = new int(l);
}
void display(){
cout << length;
}
Wall(const Wall& w2) { // deep copy
length2 = new int(*w2.length2);
cout << "Copy constructor called" << endl;
cout << *length2 << endl;
*length2 = 13;
cout << *length2;
}
};
void run() {
Wall w;
w.display();
Wall w2(w);
w2.display();
Wall w3;
w3.display();
Wall w4(w3);
w4.display();
}
}
namespace example2 {
class Segment {
int* ptr;
int m_number;
public:
Segment();
Segment(int* ptr);
// Segment(const Segment& t); // copy constructor; shallow copy
Segment(const Segment& t1);
// ~Segment();
};
Segment::Segment() {
ptr = new int(0);
cout << "Default constructor\n";
}
Segment::Segment(int* ptr){ // shallow copy
this -> ptr = ptr;
cout << "Pointer constructor,,, " << *ptr << endl;
}
Segment::Segment(const Segment& t1) {
this -> m_number = m_number;
// this -> ptr = t1.ptr; // shallow copy
int temp = *(t1.ptr); // getting the value stored in ptr and store it in temp
this -> ptr = new int(temp); // creating new memory location for ptr but with the old value.
cout << "Copy constructor is called\n";
}
// Segment::~Segment() {
// if(ptr)
// delete ptr;
// ptr = nullptr;
// cout << "Pointer is destroyed,,," << endl;
// }
Segment fun() {
Segment s;
return s;
}
void run() {
int *ptr = new int(10);
Segment s(ptr); // Pointer constructor,,, 10
// Pointer is destroyed,,,
// Segment s2(s); // copy constructor; shallow copy; bug
// Segment s2(s);
Segment s3(fun()); // accepted due to const and &
}
/*
shallow copy
----------------
s1{ptr -> 0xffaa(15), "hello", 10} --> destructor s1: delete ptr
s2(s1) --> s2{copy of s1 data} --> s2 {ptr -> 0xffaa(15), "hello", 10}
when the destructor for s2 is called, the ptr which is already deleted will be need to be deleted again!
This will raise segmentation error
The solution is to differntiate between the address of ptr in both objects,
----------
ptr of s1 in a memory locationa nd ptr of s2 is a different one in another memory location.
but both have the same value. This is the idea of deep copying. For example:
--------------
s1{ptr -> 0xffaa(15), "hello", 10}
s2 {ptr -> 0xffbb(15), "hello", 10}
To implement the deep copy, we have to initiate the copy constructor by ourselves
*/
}
namespace example3 {
class String {
private:
char* m_buffer;
unsigned int m_size;
public:
String(const char* string) {
m_size = strlen(string);
m_buffer = new char[m_size + 1];
memccpy(m_buffer, string, m_size, m_size + 1);
}
friend ostream& operator<< (ostream& stream, const String& string);
char& operator[] (unsigned int index){
return m_buffer[index];
}
// deep copying using copy constructor
String(const String& other) : m_size(other.m_size) {
cout << "Object is copied..." << endl;
m_buffer = new char[m_size];
memccpy(m_buffer, other.m_buffer, m_size, m_size + 1);
}
~String() {
delete[] m_buffer;
}
};
void printString(const String& string) {
cout << string << endl;
}
ostream& operator<<(ostream& stream, const String& string) {
stream << string.m_buffer;
return stream;
}
void run() {
String string = "Hend";
String second(string);
second[2] = 'b';
printString(string); // Hend
printString(second); // Hebd
}
}
int main() {
example1::run();
example2::run();
example3::run();
}