0

Operator Overloading doubt

Can anyone dry run and explain how two objects are passed as parameters and how the value of var and obj.var differs #include <iostream> using namespace std; 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; }

10th Jul 2018, 1:02 PM
Praful M
Praful M - avatar
3 Answers
+ 1
Compiler "translate" obj1+obj2 statement in obj1.operator+(obj2) if this is what want know
10th Jul 2018, 2:32 PM
KrOW
KrOW - avatar
+ 1
KrOW I don't understand how the two numbers are passed, how does var become 12 and obj.var become 55, are they passed together or separately
11th Jul 2018, 2:19 PM
Praful M
Praful M - avatar
0
Praful M Thanks to constructor MyClass(int a):var(a){} This copy argument ('a') value to class 'var' attribute
11th Jul 2018, 2:38 PM
KrOW
KrOW - avatar