重载字符串运算符+
Overload string operator+
我正在尝试自己制作 "String" class。但是我在重载 operator+ 时遇到了问题。我制作了运行良好的 operator += 和有时无法按我计划运行的朋友 operator+。
String()
{
length = 0;
p = new char[1];
p[0] = '[=10=]';
}
String(const char*s)
{
length = strlen(s);
p = new char[length+1];
strcpy(p, s);
}
String(const String& s)
{
if (this != &s)
{
length = s.length;
p = new char[s.length + 1];
strcpy(p, s.p);
}
}
~String()
{
delete[]p;
};
String &operator+=(const String&s1)
{
String temp;
temp.length = length + s1.length;
delete[]temp.p;
temp.p = new char[temp.length + 1];
strcpy(temp.p, p);
strcat(temp.p, s1.p);
length = temp.length;
delete[]p;
p = new char[length + 1];
strcpy(p, temp.p);
return *this;
}
friend String operator+(const String &s1, const String &s2)
{
String temp1(s1);
temp1 += s2;
return temp1;
}
如果我像这样使用运算符 + :String c =a+b;一切都按计划进行,但如果我写 a=a+b;我得到错误 String.exe 已触发断点。我应该纠正什么?
/////我解决了重载 operator= 的问题,谢谢!
String operator+=(const String&s1)
{
int temp_length = length + s1.length;
String temp(*this);
length = temp_length;
delete[]p;
p = new char[length + 1];
strcpy(p, temp.p);
strcat(p, s1.p); //<-- This is the problem
return *this;
}
friend String operator+(const String &s1, const String &s2)
{
String temp1(s1);
temp1 += s1;
return temp1;
}
在上面标记的行中,您正在访问 s1.p
,这与您所描述的问题情况下的 this.p
相同。严格禁止为两个参数调用同一个数组的 strcat。你需要再做一份。
see this answer
根据 strcat(3):
strcat() 函数将 src 字符串附加到 dest 字符串,覆盖 dest 末尾的终止空字节 ('\0'),然后添加终止空字节。 字符串不能重叠,目标字符串必须有足够的space作为结果。
建议的解决方案:
String operator+=(const String&s1)
{
int temp_length = length + s1.length;
String temp(*this);
length = temp_length;
delete[]p;
p = new char[length + 1];
strcpy(p, temp.p);
if(p == s1.p)
{
String other_temp(s1.p);
strcat(p, other_temp);
} else
strcat(p, s1.p); //<-- no longer a problem
return *this;
}
friend String operator+(const String &s1, const String &s2)
{
String temp1(s1);
temp1 += s1;
return temp1;
}
我正在尝试自己制作 "String" class。但是我在重载 operator+ 时遇到了问题。我制作了运行良好的 operator += 和有时无法按我计划运行的朋友 operator+。
String()
{
length = 0;
p = new char[1];
p[0] = '[=10=]';
}
String(const char*s)
{
length = strlen(s);
p = new char[length+1];
strcpy(p, s);
}
String(const String& s)
{
if (this != &s)
{
length = s.length;
p = new char[s.length + 1];
strcpy(p, s.p);
}
}
~String()
{
delete[]p;
};
String &operator+=(const String&s1)
{
String temp;
temp.length = length + s1.length;
delete[]temp.p;
temp.p = new char[temp.length + 1];
strcpy(temp.p, p);
strcat(temp.p, s1.p);
length = temp.length;
delete[]p;
p = new char[length + 1];
strcpy(p, temp.p);
return *this;
}
friend String operator+(const String &s1, const String &s2)
{
String temp1(s1);
temp1 += s2;
return temp1;
}
如果我像这样使用运算符 + :String c =a+b;一切都按计划进行,但如果我写 a=a+b;我得到错误 String.exe 已触发断点。我应该纠正什么? /////我解决了重载 operator= 的问题,谢谢!
String operator+=(const String&s1)
{
int temp_length = length + s1.length;
String temp(*this);
length = temp_length;
delete[]p;
p = new char[length + 1];
strcpy(p, temp.p);
strcat(p, s1.p); //<-- This is the problem
return *this;
}
friend String operator+(const String &s1, const String &s2)
{
String temp1(s1);
temp1 += s1;
return temp1;
}
在上面标记的行中,您正在访问 s1.p
,这与您所描述的问题情况下的 this.p
相同。严格禁止为两个参数调用同一个数组的 strcat。你需要再做一份。
see this answer
根据 strcat(3):
strcat() 函数将 src 字符串附加到 dest 字符串,覆盖 dest 末尾的终止空字节 ('\0'),然后添加终止空字节。 字符串不能重叠,目标字符串必须有足够的space作为结果。
建议的解决方案:
String operator+=(const String&s1)
{
int temp_length = length + s1.length;
String temp(*this);
length = temp_length;
delete[]p;
p = new char[length + 1];
strcpy(p, temp.p);
if(p == s1.p)
{
String other_temp(s1.p);
strcat(p, other_temp);
} else
strcat(p, s1.p); //<-- no longer a problem
return *this;
}
friend String operator+(const String &s1, const String &s2)
{
String temp1(s1);
temp1 += s1;
return temp1;
}