Operator overloading...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcin Vorbrodt

    Operator overloading...

    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


  • David B. Held

    #2
    Re: Operator overloading...

    "Marcin Vorbrodt" <mvorbro@eos.nc su.edu> wrote in message
    news:bkffo4$n3i $1@uni00nw.unit y.ncsu.edu...[color=blue]
    > 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?[/color]

    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).
    [color=blue]
    > I understand that operators like == or != can/should be
    > definced outside of the class in order to be symetric.[/color]

    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.
    [color=blue]
    > 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?[/color]

    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


    Comment

    • Thomas Matthews

      #3
      Re: Operator overloading...

      Marcin Vorbrodt wrote:[color=blue]
      > 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
      >
      >[/color]

      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!=(cons t AnyClass& a, const AnyClass& b)
      {
      return !(a == b);
      // or return !a.operator==(b );
      }

      --
      Thomas Matthews

      C++ newsgroup welcome message:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book
      http://www.sgi.com/tech/stl -- Standard Template Library

      Comment

      Working...