-
Notifications
You must be signed in to change notification settings - Fork 2
/
deep_copy.cpp
66 lines (56 loc) · 1.06 KB
/
deep_copy.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
#include <iostream>
#include <cassert>
#include <cstring>
using namespace std;
class MyString{
private:
char* m_data;
int m_length;
public:
MyString(const char* data=""){
assert(data);
m_length = strlen(data);
m_data = new char[m_length];
for(int i=0; i<m_length; ++i){
m_data[i] = data[i];
}
};
MyString(const MyString& source){
m_length = source.m_length;
m_data = new char[m_length];
for(int i=0; i<m_length; ++i){
m_data[i] = source.m_data[i];
}
};
MyString& operator=(const MyString& source){
if(this == &source){
return *this;
}
delete[] m_data;
m_length = source.m_length;
m_data = new char[m_length];
for(int i=0; i<m_length; ++i){
m_data[i] = source.m_data[i];
}
return *this;
}
~MyString(){
cout << "Inside destructor\n";
delete[] m_data;
};
char* get_string(void){
return m_data;
}
int get_length(void){
return m_length;
}
};
int main(int argc, char const *argv[]){
MyString hello{"Hello"};
{
MyString copy_string2;
copy_string2 = hello;
}
cout << hello.get_string() << endl;
return 0;
}