Thursday, 8 August 2013

C++ Im trying to append a character array to the string leaving the string intact with operator overloading+

C++ Im trying to append a character array to the string leaving the string
intact with operator overloading+

This is my code.. is giving me a warning saying that 'str' reference to
stack memory associated with local variable 'str' returned... Also, I
wanna be sure that my logic is good or if there a simpler way to way, I
will really appreciate some help to learn more ways.. Thanks!
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);
strcpy(tmp, s);
MyString str(tmp);
delete tmp;
return str;
}

No comments:

Post a Comment