+ 1
MyClass operator+(MyClass &obj) no entiendo el & si no esta en el main para llamar ala funcion
No entiendo porque en la sobrecarga se pone '&' antes como parametro para el objeto si no tiene sentido si se pasara por referencia deveria ser MyClass operator+(MyClass *obj) {} Ejemplo que vi en sololearn class MyClass { public: class MyClass { public: int var; MyClass() {} MyClass(int a): var(a) { } MyClass operator+(MyClass &obj) {} }; Equivalencia, puse obj y funciono igual int var; MyClass() {} MyClass(int a): var(a) { } MyClass operator+(MyClass obj) {} };
5 odpowiedzi
+ 2
MyClass operator+( MyClass& obj )
Here the overloaded `+` operator accepts MyClass instance reference, and work using the actual instance that were passed in as argument. Any change (if any was made) on the reference argument will reflect on the actual instance being passed as argument.
______________________________________
MyClass operator+( MyClass obj )
Here the overloaded `+` operator accepts MyClass instance, makes a copy of it, and works on the copy. The actual instance passed as argument will not be affected by any change (if any was made) to the copy.
+ 2
Ipang when we pass by reference we usually use the * instead of the &. That's why it's trycky I guess
+ 2
Ipang Oh I get it, thanks for the article
+ 1
I guess it's because you have to set an object as parameter which is the adress the object you call
+ 1
Kenobi
There's a difference in meaning of reference in C++ to pointers : )
https://techdifferences.com/difference-between-pointer-and-reference-2.html
http://www.differencebetween.net/technology/difference-between-pointer-and-reference/