Operator overloading...

Discussion in 'C++' started by Marcin Vorbrodt, Sep 19, 2003.

  1. I see a lot of source code where some operators are defined as class
    members, and some as friends, or just outside the class. Can someone please
    explain what the general rule of thumb is? I understand that operators like
    == or != can/should be definced outside of the class in order to be
    symetric. What about all the other ones? I read somewhere that only the
    operators that need to modify theri lvalue should be members... what are
    those operators? Please help.

    Thanx,
    Martin
     
    Marcin Vorbrodt, Sep 19, 2003
    #1
    1. Advertising

  2. "Marcin Vorbrodt" <> wrote in message
    news:bkffo4$n3i$...
    > I see a lot of source code where some operators are
    > defined as class members, and some as friends, or just
    > outside the class. Can someone please explain what the
    > general rule of thumb is?


    If you ask Scott Meyers (and Andrei Alexandrescu), every
    function that *can* be a non-member *should* be. So if
    you can implement a function in terms of the class's public
    interface, then by all means do so (unless you have to
    sacrifice an unacceptable amount of performance or
    whatever to do so).

    > I understand that operators like == or != can/should be
    > definced outside of the class in order to be symetric.


    Symmetry is only half the story (no pun intended). The
    real reason is to support implicit conversions. Namely,
    you won't get any for *this.

    > What about all the other ones? I read somewhere that
    > only the operators that need to modify theri lvalue should
    > be members... what are those operators?


    Well, for instance, operator=, and basically all of the other
    assignment operators that you might choose to define
    (like operator+=, operator*=, etc.). I find that the unary
    operators tend to be fine as members, but the non-
    modifying binary operators are better as non-members.

    Dave
     
    David B. Held, Sep 19, 2003
    #2
    1. Advertising

  3. Marcin Vorbrodt wrote:
    > I see a lot of source code where some operators are defined as class
    > members, and some as friends, or just outside the class. Can someone please
    > explain what the general rule of thumb is? I understand that operators like
    > == or != can/should be definced outside of the class in order to be
    > symetric. What about all the other ones? I read somewhere that only the
    > operators that need to modify theri lvalue should be members... what are
    > those operators? Please help.
    >
    > Thanx,
    > Martin
    >
    >


    As for operator= (assignment), it is defined inside the class.
    As far as others, it depends. Some authors (Scott Meyers for one),
    state that some methods defined as non-member functions actually
    improve encapsulation. Others state that the methods should be
    defined as member functions.

    I suggest defining fundamental operators as class methods, such as
    operator== (equality) and operator< (less than). Other operators,
    can be declared as non-member functions, such as operator!=
    and operator<= (which can be defined in terms of operator== and
    operator<, respectively). These operators can be defined using
    templates for any type that supports the fundamental operators:
    template <AnyClass>
    bool operator!=(const AnyClass& a, const AnyClass& b)
    {
    return !(a == b);
    // or return !a.operator==(b);
    }

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Sep 19, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    434
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    749
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    741
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    570
  5. Replies:
    2
    Views:
    323
Loading...

Share This Page