标签:this 深拷贝 char his ++ obj 根据 strcpy nbsp
1、等号操作符重载,实现深拷贝
//等号运算符重载 // obj3=obj1;//原始的是浅拷贝,现在要重载为深拷贝 Name& operator=(Name &obj1) { //1、先释放旧的内存 if (this->m_p != nullptr) { delete[] m_p; m_len = 0; } //2、根据 obj1 分配内存大小 this->m_len = obj1.m_len; this->m_p = new char[m_len + 1];
//3、把obj1赋值给obj3 strcpy(m_p, obj1.m_p); return *this;//返回引用,实现链式编程 }
标签:this 深拷贝 char his ++ obj 根据 strcpy nbsp
原文地址:https://www.cnblogs.com/eilearn/p/10915936.html