背景:有一个类Apple, 包括一个私有成员变量a和两个公共接口:SetA ,GetA分别是修改a和获取a。
则重载=时可以用以下两种方式:
Apple& operator= (const Apple & tem) {
this->SetA(fun.GetA());
return *this;
}
Apple* operator= (const Apple & tem) {
this->SetA(fun.GetA());
return this;
}
在两种实现方法中,"this->"都可以去掉。但是绝不能用“this.”,因为this是一个指向自己的指针。
多数情况下人们采用第一种实现方式。