+ 1

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!

31st Jan 2021, 8:27 PM
Jonathan Pollard
9 Answers
+ 5
"How does the operator 'know' about obj1" Actually, the operator knows nothing at all. It is the class which defines what happens when an operator succeeds a class instance in the code. Suppose you have two instances of MyClass `m1` and `m2`. MyClsss m1(10), m2(20); Now when you use the '+' operator on them, like so m1 + m2 the compiler actually evaluates it as m1.operator+(m2); You see? The .operator+() method of the LHS is being called and RHS is being passed into it. This is how the compiler knows that it has to add `m1`, the LHS to `m2`, the RHS. That is why, when you define the operator+() method of a class, you enable the + operator on that class. You are not limited to overloading the operator for a specific class. You can also overload operators globally, in which case it will recieve 2 arguments. For example, the following code overloads the '*' operator to enable multiplication of string and int https://code.sololearn.com/cDwp4st8Qm7L/?ref=app
31st Jan 2021, 9:08 PM
XXX
XXX - avatar
+ 2
Jonathan Pollard as you saw, you can't overload the '*' operator for int and int, because as the error message said, you can only make an overload where atleast one of the operands is a class or enumerated type. You can't overload an operator for 2 primitive types (like int) For your main question, `this` does not refer to `m1.var`, it referes to just `m1`. Actually, `this` is available as a pointer to the instance inside the declaration of any class. So that is why, `this->var` is actually the same as doing m1.var from outside the class.See this https://www.geeksforgeeks.org/this-pointer-in-c/
1st Feb 2021, 1:33 AM
XXX
XXX - avatar
+ 1
@XXX, very helpful. In your code, 's * n' makes sense because there are two arguments being passed to the operator overload function. In my case, I can see why m1.operator+(m2) applies, but I am struggling to 'think' about the general rule. Is it possible to overload the operator such that I send int a * int b and return a + b? I tried modifying your code to https://code.sololearn.com/ca25a0a22A14 but this did not work as it required a class or enumerated type. QUESTION: Does THIS in 'res.var= this->var+obj.var;' refer to m1.var? I.e this is a pointer to the current object (obj1) and is added to obj.var (obj2)?
31st Jan 2021, 9:33 PM
Jonathan Pollard
0
Yes, I understand that -> is the pointer to the individual object of the class type. Thanks for your help!
1st Feb 2021, 11:26 PM
Jonathan Pollard
0
XXX is it an implicit conversion thing done by the compiler? How is it working?
7th Jul 2021, 9:11 AM
Rishi
Rishi - avatar
0
Rishi sorry, I don't understant what you're trying to ask. "is it an implicit conversion" which line or expression are you talking about? "how is it working" how is 'what' working? Please make your question clearer (I forgot I ever answered in this thread lol. 5 months feels like ages ago)
10th Jul 2021, 6:14 AM
XXX
XXX - avatar
0
XXX the implicit conversion I'm asking about is this line "MyClass obj3=obj1+obj2" being covered to: "MyClass obj3=operator+(&obj1, obj2)" or "MyClass obj3=obj1.oprator+(obj2)" How is it working:"it" refers to the operator overloading. P.S: I know about operator overloading, just curious about how the compiler works that out. Also, see the code in the question asked above once to make my question clear
10th Jul 2021, 3:23 PM
Rishi
Rishi - avatar
0
Rishi Implicit conversion is term mostly used for conversions between types, so no, that is not the correct term. In fact, I doubt there is really any term for it. As for how it works, I don't really know how to explain it. The exact handling of it can only be told by someone who works on the compiler itself. But I guess the compiler detects a binary expression (like a + b), and then calls the corresponding function a.operator+(b). All functions used in the program are stored in memory, so I guess in the assembly code, it would only include pushing the arguments on the stack, and then calling the function.
11th Jul 2021, 8:19 AM
XXX
XXX - avatar
0
XXX oh okay, thank you
11th Jul 2021, 10:10 AM
Rishi
Rishi - avatar