0

Setting this->var and obj.var

So when I fiddled around with the sample code, I made it print out what this->var and obj.var were set to perform the operation. When main() is written as obj1+obj2, it set this->var to be the number on the left side of the operation and obj.var to be the number on the right side, which is consistent with this->var+obj.var as written in the overloaded operator function. However, upon rewriting the overloaded operator function as read obj.var+this->var, it still set the obj1 as this->var and obj2 as obj.var. Why is this? How are this->var and obj.var set? TL/DR: Basically, for overloading operator, +: res.var= obj.var+this->var; vs. res.var= this->var+obj.var; Yield the same results when printing out this->var and obj.var for obj1+obj2.

2nd Jun 2017, 8:03 PM
CiCi Pham
CiCi Pham - avatar
2 ответов
0
My question was why is obj1 always this->var and obj2 is always obj.var if this-> should always point to the most recent value.
3rd Jun 2017, 2:42 PM
CiCi Pham
CiCi Pham - avatar
- 1
-> for pointer . for variable
3rd Jun 2017, 5:29 AM
hessi
hessi - avatar