> This seems illogical and it can be cured by making op+ a non member
> function
>
> A operator+( const A& lhs, const A& rhs ) { ... }
>
> Now the compiler can use this operator as long as there is one A
> object on either side of the '+'. The other one will be converted
> to an A object if necessary.

Gernot Frisch asked:
> when Do I need the:
>
> a a:perator+(a _1);
> and when the
> a operator+(a _1, a _2)

Karl Heinz Buchegger replied:
> The first is a member function, while the second is not.
....
> A operator+( const A& lhs, const A& rhs ) { ... }

It has been suggested to implement operator+ in terms of member
operator+= (e.g., last Friday by Richard Herring, on this newsgroup).
Of course, you should have operator+= as follows:
A& A:perator+=(const A&)
{
// TODO: The addition itself!
return *this;
}

If you do, is there a difference between the following two versions?

A operator+(const A& _1, const A& _2)
{
return A(_1) += _2;
}

and:

A operator+(A _1, const A& _2)
{
return _1 += _2;
}

Which one is preferable? Will it make any difference to the caller of
operator+?

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!