#include <string.h> class MyString { public:     MyString(const char *str);     MyString(const MyString& other);     ~MyString();     MyString& operator=(const MyString& other); public:     // Never be nullptr(or NULL)     char *m_data;     void __copy(const char* str); }; MyString::MyString(const char *str) : m_data(nullptr) {     __copy(str); } MyString::MyString(const MyString& other) : m_data(nullptr) {     __copy(other.m_data); } MyString& MyString::operator=(const MyString& other) {     if (this != &other) {         delete[] m_data;         __copy(other.m_data);     }     return *this; } MyString::~MyString() {     delete[] m_data; } void MyString::__copy(const char* str) {     if (str == nullptr) {         m_data = new char[1];         m_data[0] = '\0';     } else {         int len = strlen(str) + 1;         m_data = new char[len];         strncpy(m_data, str, len);     } }
点赞 评论

相关推荐

野猪不是猪🐗:还是太卑微了,什么叫放弃本次面试应该说经过评估,贵公司与自己不匹配,决定不再推进后续流程
点赞 评论 收藏
分享
酷酷的喜马拉雅山:感觉这比一直在初筛不动的好多了
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务