//编写MyString的构造函数、拷贝构造函数、析构函数和赋值函数 #include<string.h> 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(const char*str) {     if(str==NULL)     {         m_data=new char[1];         *m_data='\0';     }     else     {         int len=strlen(str);         m_data=new char[len+1];         strcpy(m_data,str);     } } //拷贝构造函数  MyString::MyString(const MyString &other) {     int len=strlen(other.m_data);     m_data=new char[len+1];     strcpy(m_data,other.m_data); } //析构函数  MyString::~MyString(void) {     delete[] m_data; }  //赋值函数  MyString & MyString::operator =(const MyString &other) {     if(this==other)     {         return *this;     }     delete[] m_data;     int len=strlen(other.m_data);     m_data=new char[len+1];     strcpy(m_data,other.m_data);     return *this; }
点赞 评论

相关推荐

脾气小祖宗:这简历摸到都得狠狠地消毒液洗手😂
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务