String {
public:
String(const char* str);
String(const String& str);
String& operator=(const String& str);
~String();
private:
char* m_data;
};
String::String(const char* str)
{
if (str == NULL)
{
m_data = new char[1];
m_data[0] = '\0';
} else {
m_data = new char[strlen(str) + 1];
strcopy(m_data, str);
}
}
String::String(const String& str)
{
m_data = new char[strlen(str.m_data) + 1];
strcopy(m_data, str.m_data);
}
String::String& operator = (const String& str)
{
if (this == &str)
{
return *this;
}
delete[] m_data;
m_data = new char[strlen(str.m_data) + 1];
strcopy(m_data, str.m_data);
return *this;
}
String::~String()
{
delete[] m_data;
m_data = NULL;
}
参考:
[1] https://blog.csdn.net/Johnisjohn/article/details/93380152