c++请问这道题要怎么做?

来源:百度知道 编辑:UC知道 时间:2024/05/31 06:47:46
class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operate (const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
请编写String 的上述4 个函数。
这道题我还不理解?里面的赋值函数到底是用来做什么的?

这样做:
//省略了很多出错处理
String& String::operator=(const String& other)
{
//下面这两句必须
if(this == &other)
return *this;

delete[] m_data;
m_data = new char[strlen(other.m_data)+1];
strcpy(m_data, other.m_data);

return *this;
}