class MyString {
public:
MyString(const char* str = NULL) {
if (str == NULL) {
my_str = new char;
*my_str = '\0';
}
else {
my_str = new char[strlen(str)+1];
while (str != NULL) {
strcpy(my_str, str);
}
}
}
MyString(const MyString& str) {
my_str = new char[strlen(str.my_str) +1];
strcpy(my_str, str.my_str);
}
~MyString() {
delete []my_str;
}
MyString& operator =(const MyString&str) {
if (this == &str) {
return *this;
}
delete my_str;
my_str = nullptr;
my_str = new char[strlen(str.my_str) + 1];
strcpy(my_str, str.my_str);
return *this;
}
private:
char* my_str;
};
手写string类
最新推荐文章于 2024-08-13 15:35:17 发布