I'm a student who is learning C++ (especially the operator overloading part). During my study, I found the following code that uses operator overloading doesn't make an expected output. At first, the code with which I have trouble is the...
#include <iostream>
using namespace std;
class Power {
int kick;
int punch;
public:
Power(int kick = 0, int punch = 0) {
this->kick = kick; this->punch = punch;
}
void show();
Power operator << (int n);
};
void Power::show() {
cout << "kick=" << kick << ',' << "punch=" << punch << endl;
}
Power Power::operator <<(int n) {
kick += n;
punch += n;
return *this;
}
int main() {
Power a(1, 2);
a << 3 << 5 << 6;
a.show();
}
The overloaded operator <<
was designed for adding the integer to all member variables of the instance. I installed <<
operators three times in a single line such as a + 3 + 5 + 6
. I expected the output will be kick=15,punch=16
, but the console shows kick=4,punch=5
as a result of that code. It seems that only the first <<
operation works and the behind code doesn't work at all as I expected like parallelly connected math operators.
I know this unexpected behavior of the suggested code will be simply fixed by adding &
(reference) to the operator overloading like Power& operator << (int n);
, but I can't grasp why these make totally different outputs. When I utilized the debugging feature in Visual Studio, the calculated outputs since the second overloaded operation aren't applied to the target a
, but I couldn't find more answers beyond just the behavior.