0

I have a class called BigInteger which supports big integer operation. I want to implement mixture operation between BigInteger and built-in type 'int'. In other word, I want to support following statements

BigInteger a(10);
a + 10;
10 + a;

I know overloaded function can deal with it

BigInteger operator +(const BigInteger&, const int&);
BigInteger operator +(const int&, const BigInteger&);

Besides, I know a conversion operator can only deal with it ,

operator int();

But the above function support convert BigInteger to int which will lose precision. I am looking for some methods which will be simpler than overloaded function and keep precision.

Thanks, everyone.

I try it,

#include <iostream>
using namespace std;

class BigInteger
{
public:
    BigInteger(const int& i)
    {
        cout << "construct: " << i << endl;
        val = i;
    }

//    BigInteger operator +(const BigInteger& tmp) const
//    {
//        return BigInteger(val + tmp.val);
//    }

    friend ostream& operator <<(ostream& os, const BigInteger& bi)
    {
        os << bi.val << endl;
        return os;
    }

    int val;
};

BigInteger operator +(const BigInteger& a, const BigInteger& b)
{
    return BigInteger(a.val + b.val);
}

int main(int argc, const char *argv[])
{
    BigInteger a(12);
    cout << (a + 123) << endl;
    cout << (1231 + a) << endl;

    return 0;
}

why can't I use member function? How it works?

alfredtofu
  • 117
  • 1
  • 6
  • 2
    how much simpler than overloading op+ could it get? – PlasmaHH Apr 15 '13 at 09:07
  • Regarding the differences between free function and member function: http://stackoverflow.com/a/4421729/20984 . In short, prefer free functions so that `int + BigInteger` and `BigInteger + int` works equally (with a member function, the former will not work). – Luc Touraille Apr 15 '13 at 09:43

5 Answers5

4

You need to add constructor that will take value of BigInteger from int

BigInteger (const int& value)
Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
alexrider
  • 4,449
  • 1
  • 17
  • 27
3

So, make a constructor BigInteger(int), and define an BigInteger operator(const BigInteger &lhs, const BigInteger &rhs).

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
3

You should implement overloads for BigInteger operator+(BigInteger const&, int); As Herb Sutter and Andrei Alexandrescu wrote in C++ Coding Standarts (Item 29) you should "Consider overloading to avoid implicit type conversation". Your BigInteger might use operator new for construction wich can be avoided in this particular case. Additionally you should implement the binary operation in terms of unary operation:

class BigInteger {
  // ..
public:
    BigInteger& operator+=(int); // this operation can reuse BigIntegers buffer
  // ..
}

BigInteger operator+(BigInteger lhs, int rhs)
{
  return lhs+=rhs;
}

BigInteger operator+(int lhs, BigInteger rhs)
{
   return rhs+=lhs;
}
Jan Herrmann
  • 2,717
  • 17
  • 21
1

Make sure that you are only ever converting from int to BigInteger, never the reverse. Or, throw an exception if the reverse conversion would lead to an overflow.

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75
  • BTW, are there implicit and explicit convertion operators in C++? For instance, in Delphi you can define different behavior for implicit and explicit conversion. – Spook Apr 15 '13 at 09:10
  • @Spook They are new in [C++11](http://www.stroustrup.com/C++11FAQ.html#explicit-convertion). See the link for explanation on explicit constructors in C++98 as well. But you still cannot have implicits and explicits side by side. – Jirka Hanika Apr 15 '13 at 13:08
0

define the constructor BigInteger(int) and overload the operator BigInteger operator+(const BigInteger &left, const BigInteger &right).