I tested to write Apple&
as a return type of operator+()
and this gives me the same answer when I run the main
program. Is there any differences between Apple operator+
and Apple& operator+
?
class Apple
{
public:
Apple(size_t w): weight{w} {}
size_t getWeight(){return weight;}
Apple operator+(const Apple& other){
weight+=other.weight;
return *this;
}
~Apple(){weight=0;}
private:
size_t weight;
};
main program:
int main(){
Apple a{20};
Apple b{30};
a= a+b;
std::cout << a.getWeight() << std::endl;//prints 50
std::cout << b.getWeight() << std::endl;//prints 30
}
Aucun commentaire:
Enregistrer un commentaire