![]() |
#2
rjsp2014-11-28 08:38
|

String& operator+(const String &st1, const String &st2)
{
String result;
result.str = new char[st1.length() + st2.length() + 1];
std::strcat(result.str, st1.str);
std::strcat(result.str, st2.str);
return result;
}
{
String result;
result.str = new char[st1.length() + st2.length() + 1];
std::strcat(result.str, st1.str);
std::strcat(result.str, st2.str);
return result;
}
为什么会无限出错,我已经试过好几种方法了,再比如.我也重载了=号<<和>>,只要一运行到s1=s2+s3这样的合并列就会出错

String& operator+(const String &st1, const String &st2)
{
char *temp=new char[st1.len+st2.len+1];
strcat(temp, st1.str);
strcat(temp, st2.str);
String result(temp);
return result;
}
{
char *temp=new char[st1.len+st2.len+1];
strcat(temp, st1.str);
strcat(temp, st2.str);
String result(temp);
return result;
}