dmayola
dmayola

Reputation: 512

Overloading operators as friend

I have an idea of the use of the word friend, to access to a private members besides own class. For instance, I have a class A and need to access to a private method of an attribute which is of class B inside a method of A I could declare the method as friend.

However, see the following code:

#include <cstdlib>

class Coord {
  private:
    int x, y;
  public:
    Coord (int i1, int i2) : x(i1), y(i2) {
    }
    friend Coord operator- (Coord const& c1, Coord const& c2) {
        return Coord(c1.x-c2.x, c1.y-c2.y);
    }
    Coord abs() {
        return Coord(std::abs(x),std::abs(y));
    }
};

Which benefit could become overloading the operator- as a friend? I really don't see why someone could be interested in.

I have read a lot about it but I didn't get a clear idea.

Could someone write a little example where I can observe the fact?

Upvotes: 1

Views: 839

Answers (2)

Martin
Martin

Reputation: 4862

Take a look at Herb Sutters and Scott Meyers Example:

Here the summary:

First: Make operators like - non members: if you perform c = a - b to which object does minus belong ? a? b? or none. Most people agree with none hence a non member. Second: The operators need to modify private content, thus either you make a friend and or you use access functions like getters. So most people stick with the friend.

In your specific example the friend declaration is immediately followed by the definition which is the most compact way to define a global friend function.

Upvotes: 3

akaHuman
akaHuman

Reputation: 1352

When an operator function is implemented as a member function, the leftmost (or only) operand must be an object (or a reference to an object) of the operator’s class. If the left operand must be an object of a different class or a fundamental type, this operator function must be implemented as a non-member function (e.g. when overloading << and >> as the stream insertion and stream extraction operators, respectively).

A non-member operator function can be made a friend of a class if that function must access private or protected members of that class directly. Operator member functions of a specific class are called (implicitly by the compiler) only when the left operand of a binary operator is specifically an object of that class, or when the single operand of a unary operator is an object of that class.

Another reason why you might choose a non-member function to overload an operator is to enable the operator to be commutative.

For example, suppose we have a fundamental type variable, number, of type long int, and an object bigInteger1, of class HugeInteger (a class in which integers may be arbitrarily large rather than being limited by the machine word size of the underlying hardware). The subtraction operator (-) produces a temporary HugeInteger object as the difference of a HugeInteger and a long int (as in the expression bigInteger1 - number), or as the difference of a long int and a HugeInteger (as in the expression number - bigInteger1). Thus, we require the subtraction operator to be commutative (exactly as it is with two fundamental-type operands). The problem is that the class object must appear on the left of the subtraction operator if that operator is to be overloaded as a member function. So, we also overload the operator as a non-member function to allow the HugeInteger to appear on the right of the subtraction. The operator- function that deals with the HugeInteger on the left can still be a member function. The non-member function can simply swap its arguments and call the member function.

Upvotes: 2

Related Questions