1

I'm new in C++, so, please, go easy on me :) I've found two different ways to overload binary operator in c++.

The first one (from book "Object-Oriented Programming in C++", Robert Lafore):

class Distance
{
private:
    int value;

public:
    Distance() : value(0) {}
    Distance(int v) :value(v) {}

    Distance operator+(Distance) const;
};

Distance Distance::operator+(Distance d2) const
{
    return Distance(value+d2.value);
}

And another one, with using of friend funcs (from the Internet)

class Distance
{
private:
    int value;

public:
    Distance() : value(0) {}
    Distance(int v) :value(v) {}

    friend const Distance operator+(const Distance& left, const Distance& right);
};

const Distance operator+(const Distance& left, const Distance& right)
{
    return Distance(left.value + right.value);
}

All these cases make it possible to write following code like this:

Distance d1(11);
Distance d2(5);
Distance d3 = d1 + d2;

My question: what is the main difference of these cases? Maybe some advantages or disadvantages. Or some kind of "good programming manners"?

Thank you in advance for your wisdom! :)

Ayto Maximo
  • 125
  • 1
  • 9
  • 1
    This seems to be a duplicate: [SO: Operator overloading : member function vs. non-member function?](https://stackoverflow.com/q/4622330/7478597) (found by [google "c++ overload operator member function vs. function"](https://www.google.com/search?q=c%2B%2B+overload+operator+member+function+vs.+function)) – Scheff's Cat Apr 09 '19 at 11:42
  • [This canonical implementation reference](https://en.cppreference.com/w/cpp/language/operators#Canonical_implementations) might help you? – Some programmer dude Apr 09 '19 at 11:42
  • 2
    Also, for any operator `op` and operands `a` and `b`, the expression `a op b` will be translated to `a.operator op(b)` if the operator is overloaded as a member function (your first alternative) or `operator op(a, b)` if the operator is overloaded as a non-member function (your second alternative). – Some programmer dude Apr 09 '19 at 11:56

2 Answers2

2

Distance could be converted from int implicitly. Then the 2nd style makes it possible to use the opeartor+ with an object of Distance used as the right operand.

Distance d1(11);
Distance d2(5);
Distance d3 = d1 + d2; //fine
Distance d4 = d1 + 5;  //fine
Distance d5 = 5 + d1;  //fine

The 1st style only supports using opeartor+ with an object of Distance used as the left operand. i.e.

Distance d1(11);
Distance d2(5);
Distance d3 = d1 + d2; //fine
Distance d4 = d1 + 5;  //fine
Distance d5 = 5 + d1;  //fail
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • And what if there is more than 1 data member in the class? For example: int feet and float inches (instead of int value)? I can't do 5+d1 or vice versa then. So, is there a difference between two alternatives in this case? – Ayto Maximo Apr 09 '19 at 13:18
2

There are several subtle differences including:

The non member way allow to have both

42 + Distance(42);
Distance(42) + 42;

Whereas the member way only allows

Distance(42) + 42;
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • And what if there is more than 1 data member in the class? For example: int feet and float inches (instead of int value)? I can't do 42+Distance(42) or vice versa then. So, is there a difference between two alternatives in this case? – Ayto Maximo Apr 09 '19 at 13:22