假设我们有以下内容:
class StringClass
{
public:
...
void someProcessing( );
...
StringClass& operator=(const StringClass& rtSide);
...
private:
char *a;//Dynamic array for characters in the string
int capacity;//size of dynamic array a
int length;//Number of characters in a
};
StringClass& StringClass::operator=(const StringClass& rtSide)
{
capacity = rtSide.capacity;
length = rtSide.length;
delete [] a;
a = new char[capacity];
for (int i = 0; i < length; i++)
a[i] = rtSide.a[i];
return *this;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:当我们尝试将对象分配给自己时,为什么重载赋值运算符的实现会导致问题,如:
StringClass s;
s = s;
Run Code Online (Sandbox Code Playgroud)
我正在阅读的教科书(绝对C++)说,在delete [] a;"指针sa未定义之后.赋值运算符已经破坏了对象,并且程序的运行可能已经毁了."
为什么操作员损坏了s?如果我们在删除它之后立即重新启动sa,为什么这会在程序中导致我们必须重新定义函数的问题:
StringClass& StringClass::operator=(const StringClass& rtSide)
{
if (this == &rtSide)
//if the right side is the same as the left side
{
return *this;
}
else
{
capacity = rtSide.capacity;
length = rtSide.length;
delete [] a;
a = new char[capacity];
for (int i = 0; i < length; i++)
a[i] = rtSide.a[i];
return *this;
}
}
Run Code Online (Sandbox Code Playgroud)