Overlaoding Operator Help
Hi, I'm really hoping some cone can clarify my misunderstanding. In short, I am stuck on the operator overloading section of solo learn. Below is the code provided by solo code. Question1: Is "MyClass Operator+(MyClass &obj)" a function? Or is it redefining what happens when the "+" operator has two objects next to it so that it is encapsulated? Question2: I don't understand why there is only one parameter. Can't we just pass in two memory addressed of MyClass objects like so: MyClass operator+(Myclass &objOne, MyClass &objTwo) { MyClass res; res = objOne.var + objTwo.var; return res; } Question 3: Some usurer told me that in the statement " res.var = this->var+obj.var; " this.var represents obj1 in int main while obj.var represents obj2 in int main; but, there is only one parameter so I do not understand how this-> refers to obj1. Any help is greatly appreciated. I've been struggling with this for a while now. Thank you for taking the time to read this =) SOLO-LEARN CODE BELOW ------------------------------------------------------ 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 obj(12), obj2(55); MyClass res = obj1 + obj2; cout << res.var; } //output 67 _____________________________________