包括默认构造、有参构造、拷贝构造、拷贝赋值运算符、移动构造、移动赋值运算符及析构函数。
标签:运算符,otherString,实现,构造,char,length,MyString From: https://blog.csdn.net/weixin_43627682/article/details/140874900class MyString
{
MyString(const char *buffer)//有参构造
{
length = strlen(buffer);
m_char = new char[length + 1];
strcpy(m_char, buffer);
}
MyString(const MyString &otherString)//拷贝构造
{
length = otherString.length;
m_char = new char[length + 1];
strcpy(m_char, otherString.m_char);
}
MyString &operator=(const MyString &otherString)//拷贝赋值运算符
{
if (this != &otherString)
{
delete[] m_char;
length = otherString.length;
m_char = new char[length + 1];
strcpy(m_char, otherString.m_char);
}
return *this;
}
MyString(MyString &&otherString)//移动构造
{
length = otherString.length;
m_char = otherString.m_char;
otherString.m_char = nullptr;
lotherString.ength = 0;
}
MyString &operator=(MyString &&otherString)//移动赋值运算符
{
if (this != &otherString)
{
length = otherString.length;
m_char = otherString.m_char;
otherString.m_char = nullptr;
otherString.length = 0;
}
return *this;
}
~MyString()//析构
{
delete[] m_char;
length = 0;
}
private:
char *m_char;
int length;
};