Connecting Tech Pros Worldwide Help | Site Map

Operator overloading...

Marcin Vorbrodt
Guest
 
Posts: n/a
#1: Jul 19 '05
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
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Operator overloading...


"Marcin Vorbrodt" <mvorbro@eos.ncsu.edu> wrote in message
news:bkffo4$n3i$1@uni00nw.unity.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


Thomas Matthews
Guest
 
Posts: n/a
#3: Jul 19 '05

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!=(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

Closed Thread