#include<iostream>
#include<string>
using namespace std
class Mystring
{
public:
Mystring():str(nullptr), len(0)
{
}
Mystring(const char *const cstr)
{
if (cstr)
{
len = strlen(cstr);
str = new char[len + 1]; // +1 for null terminator
strcpy(str, cstr);
}
else
{
str = nullptr;
len = 0;
}
}
~Mystring()
{
delete[] str;
}
// 拷贝构造函数
Mystring(const Mystring &other)
{
len = other.len;
str = new char[len + 1];
strcpy(str, other.str);
}
// 拷贝赋值函数
Mystring &operator=(const Mystring &other)
{
if (this != &other)
{
delete[] str;
len = other.len;
str = new char[len + 1];
strcpy(str, other.str);
}
return *this;
}
const char *c_str() const
{
return str;
}
private:
int len;
char * str;
};
int main(int argc, const char *argv[])
{
return 0;
}
9.2作业
于 2024-09-03 08:54:33 首次发布