tags:

views:

64

answers:

2

I created my own big integer class, and big fraction class (which stores the numerator and denominator as 2 BigInt member variables). I overloaded the +,-,*,/ operators and the classes work fine. Now I want to extend them so that for example a BigInt can be added to an int painlessly, see below:

int     i;
BigInt  I1, I2;
BigFrac F1, F2;

I1 = i * I2;
F1 = F2 - i;
F1 = I1 + F2;  

I got BigInt to exhibit the behaviour I wanted by adding constructors for int, unsigned int, long, and unsigned long. So if I try to add a BigInt and a short, integer promotion occurs, then a conversion to BigInt, then +(BigInt, BigInt) is called. To get a short to add to a BigInt I overloaded + using templates:

template <class T> const BigInt operator+(const T& A, const BigInt& B)
{ return BigInt(A)+B; }

Now if I try to do the same thing for the BigFrac class I run into all sorts of overload resolution problems when I add the constructor BigFrac(const BigInt&).

How can I achieve the behaviour I want (as painlessly as possible)?

+2  A: 

I don't see why this needs to be a template:

template <class T> const BigInt operator+(const T& A, const BigInt& B)
{ return BigInt(A)+B; }

If you have a conversion from type T to BigInt, and you apperntly do, simply say:

const BigInt operator+(const Bigint& A, const BigInt& B)
{ return A+B; }
anon
I would tend to make the constructors for something like BigInt explicit, which would mean you do need extra template functions for the operations you want, but avoids automatic conversions elsewhere.
Pete Kirkham
Does the return type of `operator+` really need to be `const` ?
ereOn
However, in the case of a `BigInt` class, an implicit conversion from integral types might be justified.
Philipp
@ereOn: Yes, it should be, see Effective C++, Item 21.
Philipp
@Pete I wouldn't - I'd want those conversions - pretty much like I want implicit conversion from a char * to a std::string.
anon
@Phillipp: Any link ?
ereOn
@ereOn: I don't think the book is available to be linked. Here's a link from GotW: http://www.gotw.ca/gotw/006.htm Sutter admits it's arguable.
Fred Larson
@ereOn: returning a const value means that incorrect code like `(a + b) = c;` will fail to compile, like it would if `a` and `b` were builtin types, rather than compiling to a meaningless assignment to a temporary. It's actually in Item 3 of Effective C++, not Item 21.
Mike Seymour
@Neil Butterworth: You are right, my mistake. I'd defined operator+ as a member function of the BigInt class and so +(int,BigInt) failed. But when I defined it as a friend function it worked so no need for templates. This seems to solve my problem. :)If I make the conversions explicit how would I overload the operators without causing a resolution problem?
qaz
Sorry when I said "conversions explicit" I meant to say "constructors explicit".
qaz
@qaz I wouldn't want them to be explicit myself, but if they were you could keep the function the same and say things like B`igInt(42) + BigInt(7)`.
anon
@Mike: Thanks for the explanations. Makes sense indeed.
ereOn
@Neil Butterworth: I agree about not making them explicit, I was just wondering out of interest. If you were forced to use templates is there anything better than doing explicit conversions, or defining non template functions operator+(BigFrac,BigInt) and a operator+(BigInt,BigFrac). I like the idea of not using explicit conversions, but defining lots of non-template functions to get the compiler to choose the right function could quickly become a pain (especially with more classes). Is there another way to influence the choice it makes?
qaz
A: 

I think you get too many overloads if you templatize operator+. Although it's a lot of boilerplate, I'd suggest overloading it only for signed int, unsigned int, signed long int, unsigned long int, signed long long int and unsigned long long int.

Philipp