//已知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; } }
点赞 评论

相关推荐

27双非本,最近面试被挂麻了面试官说简历内容太简单了,技术栈要单独一行,各位佬有啥建议吗
LZStarV:项目太简单了,你像用什么开发的技术栈没必要写一句话,按点写就好了;有特色的比如说WebSocket、视频流这种狠狠吹,那就好看多了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务