Ok, I am working through a book and trying to learn C++ operator overloading. I created a BigInt class that takes a single int (initially set to 0) for the constructor. I overloaded the += method and it works just fine in the following code:
BigInt x = BigInt(2);
x += x;
x.print( cout );
The code will output 4. So, then I was working on overloading the global operator + using the following code:
BigInt operator+(const BigInt lhs, const BigInt rhs)
{
BigInt returnValue(lhs);
returnValue += rhs;
return returnValue;
}
This also works fine for the following code:
BigInt x = BigInt(1);
BigInt y = BigInt(5);
BigInt z = x + y;
z.print();
This prints out 6. However, when I try to execute the following code, it just doesn't work. The book doesn't explain very well and implies that it should simply work.
BigInt x = BigInt(1);
BigInt z = x + 5;
z.print();
This prints out 1. I'm not sure why z is 1 when it should be 6. I googled online and on stackoverflow but I couldn't find anyone else that was having a problem exactly like this. some were close, but the answers just didn't fit. Any help is much appreciated!