I'm trying to understand why passing by reference works and passing by value does not in a simple class definition
#include <iostream>
class Rettangolo {
int a, b;
public:
Rettangolo (Rettangolo& r) {
a = r.a;
}
Rettangolo (int _a, int _b) : a(_a), b(_b) {};
Rettangolo operator+= (Rettangolo r1);
void print() {
std::cout << a << ", " << b;
}
};
Rettangolo Rettangolo::operator+= (Rettangolo r1) {
a += r1.a;
b += r1.b;
return *this;
};
int main() {
std::cout << "Hello World!\n";
Rettangolo recta (1, 2);
Rettangolo rectb (5, 6);
recta += rectb;
recta.print ();
}
If I use (Rettangolo r1) in the parameter of operator+= as in the example above, the output is "6,2". But if I use (Rettangolo& r1) in the parameter the output is "6, 8" as I would expect