# one-way implicit conversion between two classes

I have two classes, `Polynomial` and `Fractional`:

``````#include <vector>

class Polynomial;
class Fractional;

class Polynomial {
private:
std::vector<double> coefficients;
int degree;

public:
Polynomial(std::string p);
operator Fractional() const;
friend Polynomial operator + (const Polynomial &p, const Polynomial &q);
// lots of other overloaded operators here such as -, *, /, etc.
};

class Fractional {
private:
Polynomial quotient, remainder, divider;

public:
Fractional(Polynomial q, Polynomial r, Polynomial d);
operator Polynomial() const;
friend Fractional operator + (const Fractional &p, const Fractional &q);
// lots of other overloaded operators here such as -, *, /, etc.
};
``````

`Fractional` is a class that holds a group of polynomials. It is meant to handle inexact `Polynomial` divisions of the form `A / B = Q + R / B` (where `Q` stands for quotient and `R`, remainder).

Problem is, when I add a `Fractional` object to a `Polynomial` one, the `Fractional` object always gets implicitly converted to a `Polynomial` (losing the "fractional" part), instead of the `Polynomial` getting implicitly converted to a `Fractional`.

``````int main()
{
Polynomial p("3x^2"), q("2x + 1"), r("-10");
Fractional f(p, q, r);   // 3x^2 + (-10) / (2x + 1)
Polynomial s("6x^2");

std::cout << f + s;
}
``````

Doing the math:

``````f + s = { 3x^2 + (-10) / (2x + 1) } + 6x^2
f + s = 9x^2 + (-10) / (2x + 1)
``````

But since `f` gets implicitly converted into a `Polynomial` object, I get:

``````f + s = 9x^2
// note that the fractional part "(-10) / (2x + 1)" got lost
``````

I tried removing `operator Polynomial()` from the `Fractional` class, but VS 2017 throws:

``````error C2678: binary '+': no operator found which takes a left-hand operand of type 'Fractional' (or there is no acceptable conversion)
note: could be 'Fractional operator +(const Fractional &,const Fractional&)'
note: or       'Polynomial operator +(const double,const Polynomial &)'
note: or       'Polynomial operator +(const Polynomial &,const double)'
note: or       'Polynomial operator +(const Polynomial &,const Polynomial &)'
note: while trying to match the argument list '(Fractional, Polynomial )'
``````

By the way, I know implicit conversion between two classes isn't good, though I believe it's not a big deal since `Fractional` can behave just like a `Polynomial` (having `R = 0` and `B = 1` so that `A / B = Q + R / B` becomes `A = Q`).