Skip to content

Commit ebffbf0

Browse files
committed
赋值运算符重载
1 parent 48d11b8 commit ebffbf0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// 此题仅针对C++有效
2+
3+
class Solution {
4+
public:
5+
char *m_pData;
6+
Solution() {
7+
this->m_pData = NULL;
8+
}
9+
Solution(char *pData) {
10+
this->m_pData = pData;
11+
}
12+
13+
// Implement an assignment operator
14+
Solution operator=(const Solution &object) {
15+
// write your code here
16+
if (this != &object) {
17+
delete[] m_pData;
18+
if (object.m_pData != NULL) {
19+
int len = strlen(object.m_pData);
20+
m_pData = new char[len + 1];
21+
strcpy(m_pData, object.m_pData);
22+
}
23+
else {
24+
m_pData = NULL;
25+
}
26+
}
27+
return *this;
28+
}
29+
};

0 commit comments

Comments
 (0)