0

I'm writing a class named Double which extends the built in type 'double' in c++. It has a data member of the type 'double'. For the class Double I need to overload many basic arithmetic operators like "+", "-", "*", "/". For example, the "+" operator is overloaded this way:

Relation<Double>* operator+ (Double &d2)   
// Relation is a abstract class template.
{
   /*
   ...code that do something else.
   */ 
   return new Plus<Double>(this, &d2);   // Plus is derived from Relation.
}                             
// Double + Double returns a Relation pointer.

and the "-" operator is overloaded fast the same way:

Relation<Double>* operator- (Double &d2)
{
   /*
   ...code that do something else but the same as above.
   */ 
   return new Minus<Double>(this, &d2);
}

The actual calculation is done by the member function in the Relation class. The only difference of the operators' bodies is the object initialized(Plus vs Minus). For operator that takes exactly two operands, I should alway do the overloading like this, which is duplicated and not nice. So template function comes to my mind. But the problem is, I can pass Plus or Minus as template argument, but can not pass the operator. How could I make a template or use other methods to do the overloading of these operators?

3
  • It seems that you are parsing some expression. But in that case, why use operator overloading ? Commented Feb 23, 2018 at 16:02
  • @liliscent Because I want to enable expressions like this : Double + Double - Double. And then save those relations to class "Relation". Commented Feb 23, 2018 at 16:06
  • Not totally clear, but if do something else is identical for many operators it looks like it should be delegated to a separate function. The you would have the operator declaration (that you can't do away with), one function call and a return. Looks pretty clean to me. Commented Feb 23, 2018 at 16:51

1 Answer 1

0

Yes, operator overloading may be a pain and source of code duplication, see this suggestion to the standard to ease it.

For now the only I can think about is something like this:

template<typename T>
struct OperatorMinus {
  static T doIt(T const& lhs, T const& rhs) { return lhs - rhs; };
}

template<typename T>
struct OperatorPlus {
  static T doIt(T const& lhs, T const& rhs) { return lhs + rhs; };
}

template<typename T, typename U>
class Operator: public Relation<T>
  public:
  Operator(T const& lhs, T const& rhs): _lhs(lhs), _rhs(rhs) {}

  T doIt() override {
    return U::doIt(_lhs, _rhs);
  }

  private:
  T _lhs;
  T _rhs;
};

Relation<Double>* operator+ (Double &d2)   
{
   return new Operator<Double, OperatorPlus<Double>>(this, &d2);
}   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.