How to write an assignment operator= for an object?

class person{
private:
string name;
public:
person& operator=(const person& rightValue){.......; return *this;}
}

  • Fist check the self-assignment to prevent the dangling pointer. For example, if "this" == "&rightValue", after deleting "this","rightValue" doesn't exist.
  • Second make clean-up, delete this.
  • Third make the reference to object itself is returned.

No comments:

Post a Comment