代码如下:
class String
{
public:
String(const char* str = NULL);
String(const String& other);
String& operator=(const String& other);
~String();
private:
char* m_data;
};
String::String(const char* str)
{
if(str == NULL)
{
m_data = new char[1];
m_data[0] = '\0';
}
int length = strlen(str);
m_data = new char[length +1];
strcpy(m_data,str);
}
String::String(const String& other)
{
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data,other.m_data);
}
String& String::operator=(const String& other)
{
if(this == &other)
{
return *this;
}
//先删除现有内存
if(m_data != NULL)
{
delete[] m_data;
}
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data,other.m_data);
return *this;
}
String::~String()
{
if(m_data != NULL)
{
delete[] m_data;
}
}