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)?
I don’t see why this needs to be a template:
If you have a conversion from type T to BigInt, and you apperntly do, simply say: