更新时间:2018-11-22 15:59作者:李一老师
assert(m_data != NULL);
strcpy(m_data,str);
}
}
String::String(const String &other) //拷贝构造函数
{
m_data = new char[strlen(other.m_data)+1];
assert(m_data != NULL);
strcpy(m_data,other.m_data);
}
String::~String(void) //析构函数
{
if(m_data != NULL)
{
delete []m_data;
m_data = NULL;
}
}
String & String::operator=(const String &other) //赋值函数
{
if(&other == this) //如果对象与other是同一个对象
return *this;
delete []m_data; //释放堆内存
m_data = new char[strlen(other.m_data)+1];
assert(m_data != NULL);
strcpy(m_data,other.m_data);
return *this;
}