class MString
{
public:
MString(const char* str = NULL);
MString(const MString& str); //拷贝构造
~MString();
MString& operator=(const MString &str); //赋值
private:
char* m_data;
int m_length;
};
MString::MString(const char* str)
{
if (str == NULL)
{
m_length = 0;
m_data = new char[1];
*m_data = '/0';
}
else
{
m_length = strlen(str);
m_data = new char[m_length+1];
strcpy(m_data, str);
}
}
MString::MString(const MString &str)
{
m_length = str.m_length;
m_data = new char[m_length + 1];
strcpy(m_data, str.m_data);
}
MString::~MString()
{
delete[]m_data;
m_data = NULL;
}
MString& MString::operator=(const MString& str)
{
if (this != &str)
{
MString strTemp(str);
m_length = strTemp.m_length;
m_data = new char[m_length + 1];
strcpy(m_data, strTemp.m_data);
}
return *this;
}