结论:先释放旧的内存,在返回一个引用
Name &operator=(Name &obj1)
{
//先释放旧的内存
if(m_p!=NULL)
{
delete []m_p;
m_len=0;
}
//根据obj1分配内存大小
this.m_len=obj1.m_len;
this.m_p=new char[m_len+1];
//把obj1赋值
strcpy(m_p,obj1.m_p);
return *this;
}
结论:先释放旧的内存,在返回一个引用
Name &operator=(Name &obj1)
{
//先释放旧的内存
if(m_p!=NULL)
{
delete []m_p;
m_len=0;
}
//根据obj1分配内存大小
this.m_len=obj1.m_len;
this.m_p=new char[m_len+1];
//把obj1赋值
strcpy(m_p,obj1.m_p);
return *this;
}