Skip to content

Commit

Permalink
Merge pull request #1 from dingdong9763/patch-1
Browse files Browse the repository at this point in the history
Update string.h
  • Loading branch information
yangsoon authored Jan 7, 2021
2 parents 7b80321 + 62d5bc3 commit 819a71c
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions cpp-part1/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ String::String(const String& str) {
// 首先 需要把 b清空 然后重新分配一块和a一样大的内存 将a拷贝过来
inline
String& String::operator=(const String& str) {
// 检测是不是自己 如果没有这一步 会出现错误
// 检测是不是自己 如果没有这一步 可能会出现错误 影响效率 因为第一个动作是把自己杀掉,如果a = a,那么没有东西可以拷贝了
if(this==&str) {
return *this;
}
// #1
// #1 把自己的内存空间删掉
delete[] m_data;
// #2
// #2 分配一个足够的空间
m_data = new char[strlen(str.m_data)+1];
// #3
// #3 深拷贝,把a拷贝过来
strcpy(m_data, str.m_data);
return *this;
}
Expand All @@ -81,4 +81,4 @@ std::ostream& operator << (std::ostream& os, const String& str) {
return os << str.get_c_str();
}

#endif
#endif

0 comments on commit 819a71c

Please sign in to comment.