-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_AssignmentOperator.cpp
More file actions
103 lines (94 loc) · 1.73 KB
/
Copy path1_AssignmentOperator.cpp
File metadata and controls
103 lines (94 loc) · 1.73 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
#include <iostream>
#include <cstring>
using namespace std;
class CMyString
{
public:
CMyString(char* pData = NULL);
CMyString(const CMyString& str);
~CMyString(void);
CMyString& operator= (const CMyString& str);
void print();
private:
char* m_pData;
};
CMyString::CMyString(char* pData)
{
if (pData == NULL)
{
m_pData = NULL;
}
else
{
m_pData = new char[strlen(pData) + 1];
strcpy(m_pData, pData);
}
}
CMyString::CMyString(const CMyString& str)
{
if (str.m_pData == NULL)
{
m_pData = NULL;
}
else
{
m_pData = new char[strlen(str.m_pData) + 1];
strcpy(m_pData, str.m_pData);
}
}
/*
CMyString& CMyString::operator= (const CMyString& str)
{
if (this != &str)
{
if (str.m_pData == NULL)
m_pData = NULL;
else
{
if (m_pData != NULL)
delete [] m_pData;
m_pData = new char[strlen(str.m_pData) + 1];
strcpy(m_pData, str.m_pData);
}
}
return *this;
}
*/
CMyString& CMyString::operator= (const CMyString& str)
{
if (this != &str)
{
CMyString temp(str);
char* pTemp = temp.m_pData;
temp.m_pData = m_pData;
m_pData = pTemp;
}
return *this;
}
void CMyString::print()
{
if (m_pData != NULL)
{
cout << m_pData << endl;
}
}
CMyString::~CMyString()
{
if (m_pData != NULL)
delete [] m_pData;
}
int main()
{
CMyString str1("hello");
str1.print();
CMyString str2(str1);
str2.print();
CMyString str3;
str3 = str1;
str3.print();
CMyString str4("world");
str3 = str4;
str3.print();
str1.print();
return 0;
}