关注
//已知MyString函数原型
class MyString{
public:
MyString(const char* str = NULL);
MyString(const MyString& other);
~MyString(void);
MyString& operator = (const MyString& other);
private:
char* m_data;
};
//MyString的构造函数
MyString :: MyString(const char* str){
if(NULL == str)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length + 1];
strcpy(m_data, str);
}
}
//MyString的拷贝构造函数
MyString :: MyString(const MyString& other){
int length = strlen(other.m_data);
m_data = new char[length + 1];
strcpy(m_data, other.m_data);
}
//MyString的析构函数
MyString :: ~MyString(void){
delete []m_data;
m_data = NULL;
}
//MyString的赋值函数
MyString& MyString :: operator = (const MyString& other){
if(&other == this)
{
return *this;
}
else
{
delete []m_data;
m_data = NULL;
int length = strlen(other.m_data);
m_data = new char[length + 1];
strcpy(m_data, other.m_data);
return *this;
}
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
10-24 11:39
广东工业大学 Web前端
LZStarV:项目太简单了,你像用什么开发的技术栈没必要写一句话,按点写就好了;有特色的比如说WebSocket、视频流这种狠狠吹,那就好看多了 点赞 评论 收藏
分享
10-03 16:28
梧州学院 嵌入式软件工程师 点赞 评论 收藏
分享
09-21 12:33
桂林电子科技大学 Java 点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 我是面试官,请用一句话让我破防 #
16112次浏览 100人参与
# 美团开奖 #
183432次浏览 969人参与
# “vivo”个offer #
33034次浏览 247人参与
# 校招生月薪1W算什么水平 #
15403次浏览 112人参与
# 中美关税战对我们有哪些影响 #
37806次浏览 306人参与
# i人适合做什么工作 #
7937次浏览 81人参与
# 快手技术岗信息交流阵地 #
15761次浏览 82人参与
# 读研or工作,哪个性价比更高? #
75253次浏览 762人参与
# 华为保温 #
102436次浏览 383人参与
# 哪些瞬间让你真切感受到了工作的乐趣 #
17225次浏览 79人参与
# 小厂实习有必要去吗 #
69922次浏览 344人参与
# 哪些行业值得去? #
2916次浏览 40人参与
# 秋招什么时候开投比较合适? #
109847次浏览 807人参与
# 如果秋招能重来,我会____ #
29677次浏览 255人参与
# 华为池子有多大 #
107499次浏览 748人参与
# 美团求职进展汇总 #
2806115次浏览 23836人参与
# 上班后和你想的一样吗? #
87496次浏览 666人参与
# 苦尽甘来时,再讲来时路 #
26345次浏览 359人参与
# 为了实习逃课值吗? #
23210次浏览 214人参与
# 大家实习每天都在干啥 #
97145次浏览 536人参与
# 工作压力大怎么缓解 #
119705次浏览 1112人参与
# 如果上班像打游戏,你最想解锁什么技能 #
5643次浏览 55人参与
