範例

class MyClass {
public:
MyClass& operator=(const MyClass& rhs);

private:
int field;
};


MyClassMyClass::operator=(const MyClass& rightValue)
{
if(rightValue.field == this.field){
return rightValue;
}
}

解決方案
從指派運算子傳回 *this。

class MyClass {
public:
MyClass& operator=(const MyClass& rhs);

private:
int field;
};


MyClassMyClass::operator=(const MyClass& rightValue)
{
if(rightValue.field == this.field){
return *this;
}
}