im trying to overload the operator+= and use that to implement operator+ in my string class. i keep getting a segmentation fault when i try to evaluate something like s1 += s2 where MyString s1("");and MyString s2("hello"); any suggestions as to why this is happening?
this is my operator+= function
MyString operator+=(const MyString& s1, const MyString& s2)
{
MyString temp;
delete[] temp.data;
temp.len = s1.len + s2.len;
temp.data = new char[temp.len + 1];
strcpy(temp.data, s1.data);
strcat(temp.data, s2.data);
return temp;
}
and this is my operator+ function
MyString operator+(const MyString& s1, const MyString& s2)
{
MyString temp;
delete [] temp.data;
return ((temp += s1) += s2);
}