C++ - Operator Overloading.
I am having trouble understanding how Operator Overloading works :( Any advice appreciated! Consider the code: class MyClass { public: int var; MyClass() {} MyClass(int a) : var(a) { } MyClass operator+(MyClass &obj) { MyClass res; res.var= this->var+obj.var; return res; } }; int main(){ MyClass obj1(12), obj2(55); MyClass res = obj1+obj2; cout << res.var; } QUESTION: In the operator function, I make a new object - res. How does the operator function know which object is obj1 and obj2? It would appear that I have made a THIRD object - res, in the operator function. Does 'this->var' apply to obj1 and obj.var apply to obj2? How does the operator 'know' about obj1, if I have only sent it obj2 as a parameter? Thanks!