1

I read many disscussions about operator overloading and inheritance, however there is still a point about operator overloading in child classes. My case is specific to binary arithmetic operators like *, +, - and also operators as member functions.

What is the proper way to overload an arithmetic operator in the derived class which inherits from a base class which also overload the same operator. For ex: we can call the base (copy) constructors for the parent class(es) in the derived classes in order to delegate the task to the parent(s) which can handle this task. However, I did not see something similar to this approach for these operators.

Can/Should I do all the tasks in the derived class (for ex: add or substract the members of the base class also), or somehow delegate these tasks to the base class which owns the members under consideration (or is it possible/feasible to do that?).

I have a very small and simple code in order to test this case. I have a base class named 'OpBase' and of course a derived (from Opbase) class 'OpDerived'. Both declare/define a member variable (int type). In this case, I multiply both the Base and Derived class members in the operator overload in the Derived class. What is the proper to go in that case? This question may not have an absolute answer, but doing everything in Derived class(es) seems to be a bit problematic for maintainability issues.

class OpBase
{
protected:
    int last;

    explicit OpBase() { last = 1; }
    explicit OpBase(int la) { this->last = la; }

    OpBase operator*(const OpBase &rhs) {

        OpBase op;
        op.last *= rhs.last;

        return op;
    }
};


class OpDerived: public OpBase
{
public:
    int first;

    explicit OpDerived(): first(1) { }

    OpDerived operator*(const OpDerived &rhs) {

        OpDerived op;

        op.first *= rhs.first;
        op.last *= rhs.last;

        return op;
    }

    OpDerived(const OpDerived &rhs):OpBase(rhs) {
        this->first = rhs.first;
    }
};
Deniz
  • 858
  • 10
  • 31
  • Seems to me that this belongs to codereview.stackexchange.com – eerorika Aug 25 '14 at 13:53
  • user2079303, in fact I asked this question in order to search for the reasons behind best practices. For operator overloading of course there are some guidelines like http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html, but I wonder what are the reasons to follow them. I just tried to explain the question with a simple code. Moreover, http://stackoverflow.com/questions/15943781/c-operator-overloading-for-pointers has useful answers for my question I think. – Deniz Aug 28 '14 at 15:12

1 Answers1

0

It's difficult to delegate operator* (because it manufactures a new instance), but it's easy to delegate operator*= (because it works in-place and doesn't lead to slicing). So, just implement the former consistently in terms of the latter.

OpBase& OpBase::operator*=(const OpBase& other) {
  // Actual logic goes here.
  last *= other.last;
  return *this;
}

// Could also be made a non-member with two parameters
OpBase OpBase::operator*(const OpBase& other) {
  // Standard boilerplate.
  OpBase ret(*this);
  ret *= other;
  return ret;
}

OpDerived& OpDerived::operator*=(const OpDerived& other) {
  OpBase::operator*=(other);
  // Additional logic specific to OpDerived goes here.
  first *= other.first;
  return *this;
}

// Could also be made a non-member with two parameters
OpDerived OpDerived::operator*(const OpDerived& other) {
  // Standard boilerplate again.
  OpDerived ret(*this);
  ret *= other;
  return ret;
}
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85