仿照string完成myString类
class mystring {
private:
char* data;
std::size_t size;
public:
// 构造函数
//无参
mystring() : size(10) {
data = new char[size];
}
//有参
mystring(const char* str) {
size = strlen(str);
data = new char[size + 1];
strncpy(data, str, size + 1);
}
//拷贝
mystring(const mystring& other) {
size = other.size;
data = new char[size + 1];
strncpy(data, other.data, size + 1);
}
// 拷贝赋值
mystring& operator=(const mystring& other) {
if (this != &other) {
delete[] data;
size = other.size;
data = new char[size + 1];
strncpy(data, other.data, size + 1);
}
return *this;
}
// 析构函数
~mystring() {
delete[] data;
}
//判空函数
bool empty() const {
return size == 0;
}
//size
std::size_t size1() const {
return size;
}
// c_str
const char* c_str() const {
return data;
}
};