My question is that why this->var will automatically pick 12, and obj.var just pick 55? Why this->var is not 55? Is it because that the position it is placed?(I mean this->var+obj.var where this-> is placed in front of the "+" sign) and if I change the code like this: res.var= obj.var-this->var; , it seems that this->var is still 12. So it seems like the cod obj1+obj2, if obj2 is "after" the "+" sign, it is the one which is thrown to the &obj, is that the correct inference? Thanks!
#include <iostream>
using namespace std;
class MyClass {
public:
int var;
MyClass() { }
MyClass(int a)
: var(a) { }
MyClass operator+(MyClass &obj) {//here's my problem
MyClass res;
res.var= this->var+obj.var;//here's my problem
return res;
}
};
int main() {
MyClass obj1(12), obj2(55);
MyClass res = obj1+obj2;//here's mt problem
cout << res.var;
}