65,202
社区成员




class mystring
{
public:
mystring(const TCHAR *pStr = NULL);
~mystring();
mystring(const mystring &other);
mystring &operator =(const mystring &other);
mystring &operator +=(const mystring &other);
friend const mystring operator+(const mystring &lhs, const mystring &rhs);
private:
TCHAR *mData;
};
mystring::mystring(const TCHAR *pStr /* = NULL */)
{
if(!pStr)
{
mData = new TCHAR[1];
*mData = '\0';
}
else
{
mData = new TCHAR[strlen(pStr)+1];
strcpy(mData,pStr);
}
}
mystring::~mystring()
{
delete[] mData;
}
mystring::mystring(const mystring &other)
{
mData = new TCHAR[strlen(other.mData)+1];
strcpy(mData,other.mData);
}
mystring& mystring::operator =(const mystring &other)
{
/*if(this != &other)
{
delete []mData;
mData = new TCHAR[strlen(other.mData)+1];
strcpy(mData,other.mData);
}
*/
TCHAR *OriginalData = mData;
mData = new TCHAR[strlen(other.mData)+1];
strcpy(mData,other.mData);
delete []OriginalData;
return *this;
}
mystring& mystring::operator +=(const mystring &other)
{
TCHAR *OriginalData = mData;
mData = new TCHAR[strlen(OriginalData) +strlen(other.mData)+1];
strcpy(mData,OriginalData);
strcat(mData,other.mData);
delete []OriginalData;
return *this;
}
const mystring operator+(const mystring &lhs, const mystring &rhs)
{
mystring temp(lhs);
temp += rhs;
return temp;
}