+ 3
Anyone please explain what happens when we write Myclass res=obj1+obj2; I mean how the values are passed and how it works
#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; }
1 Odpowiedź
+ 3
Myclass res = obj1+obj2 is similar to
Myclass res = obj1.operator+(obj2)
That means obj1 is used to call the function 'operator+' whereas obj2 is passed as an argument.