林锐错了?

liumang9527 liumang9527 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
六、编写类String的构造函数、析构函数和赋值函数(25分)
已知类String的原型为:
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(const String &other) // 3分
{
int length = strlen(other.m_data); //??private:m_data
m_data = new char[length+1]; // 若能加 NULL 判断则更好
strcpy(m_data, other.m_data);
}
---------------------------------------------------------
other.m_data 能直接写?
...全文

相关推荐

评论 12
收藏 已收藏
分享
返回顶部
liumang9527
C++ 语言 65,214 成员 250,514 内容