#include <cstring> class MyString { public:     MyString(const char *str = nullptr);     MyString(const MyString &other);     ~MyString(void);     MyString & operator = (const MyString & other); private:     char *m_data; }; MyString::MyString(const char *str) {     if (str) {         m_data = new char[strlen(str) + 1];         strcpy(m_data, str);     }     else {         m_data = nullptr;     } } MyString::MyString(const MyString &other) {     if (other.m_data) {         m_data = new char[strlen(other.m_data)];         strcpy(m_data, other.m_data);     }     else {         m_data = nullptr;     } } MyString::~MyString() {     if (m_data) {         delete[]m_data;         m_data = nullptr;     } }; MyString &MyString::operator= (const MyString & other) {     if (&other == this)         return *this;     if (m_data) {         delete m_data;         m_data = nullptr;     }     if (other.m_data) {         m_data = new char[strlen(other.m_data)];         strcpy(m_data, other.m_data);     }     return *this; }
点赞 评论

相关推荐

千千倩倩:简历问题有点多,加v细聊
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务