-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCString.h
More file actions
153 lines (133 loc) · 3.37 KB
/
CString.h
File metadata and controls
153 lines (133 loc) · 3.37 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
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
#ifndef _CSTRING_H_
#define _CSTRING_H_
class CString
{
public:
CString();
~CString();
CString(const char* ch);
CString(int n,const char ch);
CString(const CString& str);
//在头文件中,单行代码,程序自动归为Inline内联函数
int Getlength(){return m_length;}
operator const char*(){return m_phead;}
char operator[](int index) const{return *(m_phead+index);}
//1.针对string类的函数重载,变量分为CString&以及char*两类
//=运算符体现了赋值与初始化的区别
CString& operator=(const CString& str);
CString& operator=(const char* ch);
CString& operator+=(const CString& str);
CString& operator+=(const char* ch);
//friend CString operator+(const CString&str1,const CString& str2);
CString& operator+(const CString& str) const;
CString operator+(const char* ch) const;
private:
char * m_phead;
int m_length;
};
CString::CString()
{
m_phead=new char(0);
m_length=0;
}
CString::~CString()
{
if (m_phead!=nullptr)
{
delete[] m_phead;
}
}
//const修饰指针分为指针常量与常量指针,指针常量,const修饰常量,变量值无法被改变;
//而常量指针,则const修饰指针,仅仅无法通过指针去修改变量值。
CString::CString(const char* ch)
{
m_length=strlen(ch);
m_phead=new char[m_length+1];
//常量指针无法赋值非常量指针,但可以取值逐个赋值
//strcpy(m_phead,ch);
char *rect=m_phead;
//字符串的赋值运算符必须将‘/0’位赋值给另一个字符串
while (*rect=*ch)
{rect++;ch++;};
}
CString::CString(const CString& str)
{
m_length=str.m_length;
m_phead=new char[m_length+1];
//m_phead=str.m_phead;
strcpy(m_phead,str.m_phead);
}
CString& CString::operator+=(const char * ch)
{
m_length+=strlen(ch);
//2.字符串的实际存储控件应该为m_length+1,结束符‘\0’
char* pNew=new char[m_length+1];
//strcpy(pNew,m_phead);
//strcat(pNew,ch);
//3.对指针存取临时变量,是为了访问指针数据同时,不改变原指针
char* new_temp=pNew;
char* old_temp=m_phead;
while (*new_temp=*old_temp)
{new_temp++;old_temp++;}
while (*new_temp=*ch)
{new_temp++;ch++;}
delete[] m_phead;
m_phead=pNew;
return *this;
}
CString& CString::operator+=(const CString& str)
{
//4.在指针变量重置时,需要考虑该指针之前是否存有数据,需要清空后再赋值
m_length+=str.m_length;
char* pNew=new char[m_length+1];
strcpy(pNew,m_phead);
strcat(pNew,str.m_phead);
delete[] m_phead;
m_phead=pNew;
return *this;
}
//深复制
CString& CString::operator=(const char* ch)
{
m_length=strlen(ch);
char* pNew=new char[m_length+1];
strcpy(pNew,ch);
delete[] m_phead;
m_phead=pNew;
return *this;
}
CString& CString::operator=(const CString& str)
{
m_length=str.m_length;
char* pNew=new char[m_length+1];
//pNew=str.m_phead;
strcpy(pNew,str.m_phead);
delete[] m_phead;
m_phead=pNew;
return *this;
}
CString& CString::operator+(const CString& str) const
{
//5.临时变量作为返回值时,会发生两个步骤:1.在本类中,发生拷贝构造;2.变量销毁。
//因此若类中含有指针变量,则拷贝构造必须为深拷贝
static CString t1;
t1.m_length=m_length+str.m_length;
char *pNew=new char[t1.m_length+1];
strcpy(pNew,m_phead);
strcat(pNew,str.m_phead);
delete[] t1.m_phead;
t1.m_phead=pNew;
return t1;
}
CString CString::operator+(const char* ch) const
{
CString t;
t.m_length=this->m_length+strlen(ch);
char* pNew=new char[t.m_length+1];
strcpy(pNew,this->m_phead);
strcat(pNew,ch);
delete[] t.m_phead;
t.m_phead=pNew;
return t;
}
#endif