Re: Basic question on operator overloading
Alf P. Steinbach wrote:
* pauldepstein@att.net:
[on the issue as to whether operator+ better be a member or free-standing]
Perhaps you haven't seen enough of the code to form an opinion, but I
believe that the member version should be commented out/ deleted.
(Before, I said this about the non-member version.)
Lack of code doesn't prevent me from forming an opinion on that, but
Kai-Uwe Bux (IIRC) argued earlier (half a year ago? earlier?) that it
actually made sense to have operator+= as non-member and operator+ as
member. And so, now I'm not sure which to prefer. This choice might be
akin to the choice of some particular style of formatting, in that it
can be argued at length, without any clear conclusion.
IIRC, the discussion back then was on the issue whether operator+ better be
defined in terms of operator+= or vice versa.
With respect to the issue currently under discussion, I would favor (as of
today) a free-standing version declared outside class scope. It is more
symmetric with regard to conversions (argued elsethread). E.g.:
#include <iostream>
#include <ostream>
struct A {
friend
A operator+ ( A const & lhs, A const & rhs ) {
std::cout << "friend\n";
return ( lhs );
}
};
// the following line pulls the function into global scope:
A operator+ ( A const & lhs, A const & rhs );
struct C {
operator A ( void ) {
return A();
}
};
int main ( void ) {
C c1;
C c2;
c1 + c2; // this depends on global scope
}
BTW, I wonder why the above compiles. To me, it looks as though the
conversion function should create non-const temporaries which should not
bind to the parameters. Apparently, that is an illusion and implicit
conversions are different.
Best
Kai-Uwe Bux