Re: Overload operator * as member function.
On 11 July, 17:36, AY <techreposit...@gmail.com> wrote:
class A
{
const A operator*(const A& rt, const A& lt)
{
return A(rt.denominator()*lt.denominator(),
rt.numerator()*lt.numerator());
}
As a member you only want the right hand parameter:
const A operator*(const A& rhs); // The lhs is implicitly "this".
Always implement binary operators as free functions as otherwise
expressions like ( 3 + A(1,2) ) wont compile because the compiler will
not try to convert the 3 to an A when looking for a function match.
Always implement binary operators (op) in terms of a member method
operator op= as follows:
A& A::operator *= (const A& rhs) { blah blah; return *this; }
A operator* (const A& lhs, const A& rhs) { A temp(lhs); temp *= rhs;
return temp; }
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]