Problems:
. , , . - , undefined.
CString& operator = (const CString& rhs)
{
cout << "Assignment operator called!"<<endl;
if (this != &rhs)
{
len= rhs.len;
delete[] buff;
buff= new char[len+1];
strcpy_s(buff, len+1, rhs.buff);
}
return *this;
}
, ( ).
CString& operator = (const CString& rhs)
{
cout << "Assignment operator called!"<<endl;
if (this != &rhs)
{
char* tmp = new char[len+1];
strcpy_s(tmp, rhs.len+1, rhs.buff);
len= rhs.len;
std::swap(tmp,buff);
delete tmp;
}
return *this;
}
idium:
CString& operator = (CString rhs)
{
this->swap(rhs);
}
void swap(CString& rhs)
{
std::swap(len, rhs.len);
std::swap(buff, rhs.buff);
}
+
CString operator + (const CString& rhs) const
{
CString result(*this);
return result += rhs;
}
CString operator += (const CString& rhs)
{
size_t lenght= len+rhs.len+1;
char* tmp = new char[lenght];
strcpy_s(tmp, lenght, buff);
strcat_s(tmp, lenght, rhs.buff);
std::swap(len, length);
std::swap(buff, tmp);
delete tmp;
}