views:

234

answers:

4

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!

+2  A: 

You need an overload for adding an int to BigInt; the constant 5 in your example is of type int, not BigInt. Something like this should work:

BigInt operator+(const BigInt lhs, const int rhs)
{
    BigInt returnValue(rhs);
    returnValue += lhs;
    return returnValue;
}

You might want one for operator+(const int lhs, const BigInt rhs) too.

kevingessner
That the code compiled implies that the conversion to `BigInt` worked (by way of implicit conversion). Why should the code compile and print `1`? That does not make sense to me.
Johannes Schaub - litb
Yes, unless there is a non-`explicit` constructor that takes `int`, i.e. provides for conversion from `int` to `BigInt`, you need three overloads of `operator+`. @kevingessner, `const` before `int` is kind of useless in the parameter list. On the other hand, the return type should probably be `const BigInt` to avoid nonsense like `a + b = c;`.
Nikolai N Fetissov
I tried to above solution, and I still got the same answer.
John
+3  A: 

most likely problem is in += operator. Post code for it.

aaa
Well, @Johannes Schaub was right when he said it was being converted by way of implicit casting (a lot of breakpoints and code tracing found that one out for me). The problem was actually in the += operator. I couldn't have figured that out with out the help of earlier comment and @aaa. Thanks Both!
John
A: 

The code you've posted looks fine and should work. Problems you're seeing are almost certainly due to the copy constructor or assignment operator of your BigInt class.

Chris Dodd
+1  A: 

The following super-simplified code (the minimum I can add to include all your code and make it into a valid stand-alone executable program):

#include <iostream>

class BigInt
{
  public:
    BigInt(int i): _i(i) {}
    void print() { std::cout << "BigInt(" << _i << ")\n"; }
    void operator +=(const BigInt rhs) { _i += rhs._i; }
  private:
    int _i;
};

BigInt operator+(const BigInt lhs, const BigInt rhs)
{
    BigInt returnValue(lhs);
    returnValue += rhs;
    return returnValue;
}

int main() {
  BigInt x = BigInt(1);
  BigInt y = BigInt(5);
  BigInt z = x + y;
  z.print();

  BigInt ax = BigInt(1);
  BigInt az = ax + 5;
  az.print();

  return 0;
}

emits, as predictable:

BigInt(6)
BigInt(6)

Please make the minimum possible alterations to this working code to reproduce the bug you observe -- that will of course show where your bug exactly lies.

Alex Martelli