这是我的密码..。给我一个警告,说'str‘引用与局部变量'str’相关联的堆栈内存返回.此外,我想确定我的逻辑是好的,或者如果有一个更简单的方法,我会非常感激一些帮助学习更多的方法。谢谢!
void CopyString(char *s)
{
delete szArr;
if (s)
{
szArr = new char[strlen(s)+1];
strcpy(szArr,s);
}
else
{
szArr = new char[1];
szArr[0]=0;
}
}
MyString& operator+(char *s){
if (!s)
return *this;
char *tmp=new char[strlen(szArr)+strlen(s)+1];
strcpy(tmp, szArr);
strcat(tmp, s);
MyString str(tmp);
delete tmp;
return str;
}发布于 2013-08-08 23:25:46
您正在返回对str的引用,它是函数中的一个局部变量。
返回一个副本:MyString operator+(char *s)。
发布于 2013-08-08 23:44:20
另一个小错误是strcpy(tmp,s),它不是strcat的correct.Change strcpy可能是正确的。
https://stackoverflow.com/questions/18137865
复制相似问题