class mystring
{
public:
mystring()
{
m_pstr=nullptr;
}
mystring(mystring const &other)
{
if(other.m_pstr)
{
int sl=strlen(other.m_pstr);
m_pstr=new char[sl + 1];
strcpy_s(m_pstr, sl+1, other.m_pstr);
}
else
{
m_pstr=nullptr;
}
}
mystring& operator =(mystring const &other)
{
if(m_pstr)
{
delete[] m_pstr;
m_pstr=nullptr;
}
if(other.m_pstr)
{
int sl=strlen(other.m_pstr);
m_pstr=new char[sl + 1];
strcpy_s(m_pstr,sl+1,other.m_pstr);
}
else
{
m_pstr=nullptr;
}
return *this;
}
~mystring()
{
if(m_pstr)
{
delete[] m_pstr;
m_pstr=nullptr;
}
}
protected:
char *m_pstr;
};
MyString 四个必须具备的函数 无参构造 拷贝构造 赋值 析构
最新推荐文章于 2024-07-25 21:43:46 发布