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; }
3 odpowiedzi
+ 1
Compiler "translate" obj1+obj2 statement in
obj1.operator+(obj2)
if this is what want know
+ 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
0
Praful M Thanks to constructor
MyClass(int a):var(a){}
This copy argument ('a') value to class 'var' attribute