Discussion:
Why all the friending? (A4Q2)
(too old to reply)
acchow
2011-06-30 18:48:28 UTC
Permalink
Why do we do all the friending to operator==, operator!=, operator<,
etc. in the Rationalnumber class? What is the advantage to having these
operator overloads as non-member functions instead of non-static member
functions? It seems an unnecessary exposure of implementation.
Jeremy Roman
2011-06-30 19:41:34 UTC
Permalink
Post by acchow
Why do we do all the friending to operator==, operator!=, operator<,
etc. in the Rationalnumber class? What is the advantage to having these
operator overloads as non-member functions instead of non-static member
functions? It seems an unnecessary exposure of implementation.
Most of the operator overloads could be implemented as member functions
instead; the differences between the two approaches aren't that great.

Some people like the parallel structure of getting two arguments of the
same type instead of being in the scope of one and getting the other as
an argument.

I'm not sure what you mean when you say that it "exposes implementation"
more than using member functions; could you elaborate on that? The
functions have the same level of access if they're non-member friend
functions or member functions.

The two that cannot be done as members of Rationalnumber (as far as I
can think of) are the implementations of operator>> and operator<< here,
since they would have to be members of std::istream and std::ostream,
respectively.

This kind of approach might also be handy if we wanted to support, for
instance, adding Rationalnumber to an int (e.g. 4 + r, where r is a
rational number) or some other behaviour where the left operand is not
an object of our class.
--
Jeremy Roman
Student, Computer Science
University of Waterloo
Loading...