-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.cpp
70 lines (62 loc) · 1.23 KB
/
test1.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
#include <iostream>
#include <cstring>
using namespace std;
class CMyString
{
public:
CMyString(char* pData = nullptr)
{
if(pData == nullptr)
m_pData = new char('\0');
else
{
m_pData = new char[strlen(pData)+1];
strcpy(m_pData, pData);
}
}
CMyString(const CMyString& str)
{
m_pData = new char[strlen(str.m_pData) + 1];
strcpy(m_pData, str.m_pData);
}
CMyString& operator=(const CMyString& str)
{
CMyString temp{str};
std::swap(this->m_pData, temp.m_pData);
return *this;
}
CMyString& operator=(CMyString&& str)
{
std::swap(*this, str);
return *this;
}
~CMyString()
{
delete m_pData;
}
void print() const
{
cout << string(m_pData) << endl;
}
private:
char* m_pData;
};
int main()
{
char* pchar = new char[10];
for(int i=0; i<9; ++i)
{
*(pchar+i) = 'a' + i;
}
*(pchar + 9) = '\0';
CMyString cstr1{pchar};
CMyString cstr2;
cstr2.print();
cstr2 = cstr1;
cout << string(pchar) << endl;
cstr1.print();
cstr2.print();
cstr2 = cstr2;
cstr2.print();
return 0;
}