class String
{
public:
String(const char *str = NULL);
String(const String &rhs);
String &operator=(const String &rhs);
~String();
private:
char *m_str;
}
String::String(const char *str)
{
if(str == NULL)
{
m_str = new char[1];
m_str[0] = '\0';
}
else
{
m_str = new char[strlen(str)+1];
strcpy(m_str, str);
}
}
String::String(const String &rhs)
{
m_str = new char[strlen(rhs.m_str)+1];
strcpy(m_str, rhs.m_str);
}
String &String::operator=(const String &rhs)
{
if(this == &rhs)
return *this;
delete []m_str;
m_str = new char[strlen(rhs.m_str)+1];
strcpy(m_str, rhs.m_str);
return *this;
}
String::~String()
{
delete []m_str;
}
String类中构造函数、拷贝构造函数、赋值函数的实现
最新推荐文章于 2020-09-13 15:44:47 发布