After I overloaded <
, ==
, +
, -
, *
... in a class named Fraction
:
class Fraction:
public:
// constructor, operator= ... defined before
bool operator<(const Fraction& A) const;
bool operator==(const Fraction& A) const;
Fraction operator+(const Fraction& A) const;
Fraction operator-(const Fraction& A) const;
Fraction operator*(const Fraction& A) const;
Fraction operator/(const Fraction& A) const;
I could use these above 'basic' operators to overload >
, +=
, ++
and +
from left...
class Fraction:
public:
bool operator>(const Fraction& A) const {
return A < *this;
}
bool operator!=(const Fraction& A) const {
return !(*this == A);
}
Fraction &operator+=(const Fraction& A) const {
return *this = *this + A;
}
Fraction operator++() const {
return *this += 1;
}
friend Fraction &operator+(const int& a, const Fraction& A) const {
return A + a;
}
However, I also have classes like RealNumber
, Complex
... They also need overloading, but only overloading <
, ==
, +
, -
, *
... differs, while overloading >
, +=
, ++
... is similar. (just a typename difference)
So I'm curious about an elegant way to reduce the similar part, I've learnt the CRTP that possible helps, but it only overloads comparison...