-
Notifications
You must be signed in to change notification settings - Fork 6
/
01_AssignmentOperator.cpp
55 lines (45 loc) · 1.09 KB
/
01_AssignmentOperator.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
#include <iostream>
#include <cstring>
using namespace std;
class CMyString {
public:
CMyString(const char *pData = NULL);
CMyString(const CMyString& str);
CMyString& operator=(const CMyString& str);
~CMyString(void);
private:
char *m_pData;
};
CMyString::CMyString(const char *pData): m_pData(NULL) {
if(pData != NULL) {
m_pData = new char[strlen(pData)+1];
strcpy(m_pData, pData);
}
}
CMyString::CMyString(const CMyString& str): m_pData(NULL) {
if(str.m_pData != NULL) {
m_pData = new char[strlen(str.m_pData)+1];
strcpy(m_pData, str.m_pData);
}
}
CMyString& CMyString::operator=(const CMyString& str) {
if(this != &str) {
CMyString strTemp(str);
char *temp = strTemp.m_pData;
strTemp.m_pData = m_pData;
m_pData = temp;
}
return *this;
}
CMyString::~CMyString(void) {
if(m_pData != NULL) {
delete[] m_pData;
m_pData = NULL;
}
}
int main(int argc, char *argv[]) {
CMyString str1("hello, world!");
CMyString str2 = "I am klb3713.";
str1 = str2;
return 0;
}