What's good to implement operator overloading with 'friend'?

This is a discussion on What's good to implement operator overloading with 'friend'? within the C++ Programming forums, part of the General Programming Boards category; We all know how to implement operator overloading as the following: Code: class complex { public: complex(double r, double i){ ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    What's good to implement operator overloading with 'friend'?

    We all know how to implement operator overloading as the following:
    Code:
    class complex
    {
    public:
    complex(double r, double i){
    real = r, imag = i;}
    complex operator +(const complex & c);
    };
    But sometimes I see people use friend methods:


    Code:
    class complex
    {
    public:
    complex(double r, double i){
    real = r, imag = i;}
    friend complex operator +(const complex &c1, const complex &c2);
    };

    Why do they do this way? What're the advantages?
    Thank you.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    http://www.parashift.com/c++-faq-lite/friends.html

    Specifically 14.3 and 14.5. In fact, 14.5 also uses a complex class operator+ as an example. Here is that example in code:
    Code:
    class complex
    {
      double real;
      double imag;
    public:
      complex(double r, double i = 0.0) real(r), imag(i) { }
      friend complex operator +(const complex &c1, const complex &c2);
    };
    
    // ...
    
    complex a(1.0, 1.0);
    complex b = 2.0 + a;
    That last line won't work unless you implement via a non-member function, which often needs to be a friend.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The best way to overload an operator is as a non-friend non-member function, i.e. implementing it purely in terms of the public interface of the class.

    There are situations where that's not possible.

    Some operators can only be overloaded as members. (=, for example)
    Some operators need access to the internal representation, i.e. private data.

    In the case of the compound assignments like +=, -=, they should then be members.
    Then the simple operation (+, -) can be implemented in terms of the compound assignment and thus be non-member non-friend.
    If, however, there is a reason not to use +=, + and friends should be implemented as free functions that are friends: they have access to private data, and conversions work on both parameters.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compiler doesn't recognize my friend
    By pheres in forum C++ Programming
    Replies: 6
    Last Post: 12-05-2008, 10:10 AM
  2. Is this a missusing of the friend keyword ?
    By force of will in forum C++ Programming
    Replies: 8
    Last Post: 11-14-2007, 12:28 AM
  3. Friend cannot inherit from private subclass
    By MWAAAHAAA in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2006, 03:44 PM
  4. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 10:39 PM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21