0

I have a Fraction class that I want to be able to typecast to a double. Is there a way to write that explicitly? (Like operator overloading)

This is my constructor:

Fraction::Fraction(double n, double d) : numerator(n), denominator(d)
{
    if (d == 0) throw Error::DIVIDE_BY_ZERO;
}

When overloading operators, e.g.:

bool operator==(const Fraction& left, const Fraction& right)

I want that overload to be able to take a double as well without having to write overloads with (const Fraction&, const double) and (const double, const Fraction&)

snzm
  • 139
  • 1
  • 1
  • 10

1 Answers1

0

I would use template specialization for this task instead of cast_operator.

template<typename T>
friend bool operator==(const T& left, const T& right) // member template

Also you should implement your operator overloading as free function (with friend) otherwise you cannot pass two arguments to it.

Please refer to operator == overloading

bogdan tudose
  • 1,064
  • 1
  • 9
  • 21