1

Possible Duplicate:
Operator overloading
Operator overloading : member function vs. non-member function?

After many years of apparently abusing this construct somebody pointed out to me that this is bad practice:

class SomeClass
{
    ...
    bool operator<(const SomeClass& other) const;
};

whereas this is good practice:

class SomeClass
{
    ...
};
bool operator<(const SomeClass& a, const SomeClass& b);

But I can't for the life of me figure out why and can't find any docs on the difference. Can anyone point me in the right direction?

Community
  • 1
  • 1
smocking
  • 3,467
  • 16
  • 22
  • 1
    See http://stackoverflow.com/questions/4622330/operator-overloading-member-function-vs-non-member-function – dirkgently Apr 25 '12 at 03:29
  • Thanks, guess I was specifically searching for comparison operators and missed it. – smocking Apr 25 '12 at 03:31
  • 1
    [The Decision between Member and Non-member](http://stackoverflow.com/questions/4421706/operator-overloading/4421729#4421729). – Jesse Good Apr 25 '12 at 03:36
  • 1
    Bad practice is a bit strong. You must understand the differences and make a choice on which to use. The non member function version allows auto conversion of the LHS parameter which may or may not be a good thing depending on the situation. – Martin York Apr 25 '12 at 04:22

1 Answers1

3

The first thing is that there is no advantage in implementing the operator as a member function, and there can be advantages in doing it as a free function. In particular, a member function is not symmetric with respect to the types of the two operands, the left-hand-side (lhs) must be of the exact type of the class on which it is being called, while the right-hand-side (rhs) can use implicit conversions. In the case of free function operators, the same conversions can be applied to lhs and rhs.

struct SomeClass {
    SomeClass( int value );
    bool operator<( SomeClass const & ) const;
};
bool operator>( SomeClass const &, SomeClass const & );
int main() {
   SomeClass x( 10 );
   x < 100;             // Fine, lhs is SomeClass
   // 100 < x;          // Error, no operator< can take int as lhs and SomeClass
   x > 100;             // Fine
   100 > x;             // Also fine, lhs can take the same implicit conversions
}

Note that the main difference here are the implicit conversions, and that makes a difference only if your type can be implicitly converted from other types. If there are no implicit conversions into your type, then this point would be moot, but considering that there is no disadvantage in using a free function and there are advantages in some cases, I would use free functions whenever possible.

I wrote some time ago about operator overloading here, you might find some suggestions there.

David Rodríguez - dribeas
  • 192,922
  • 20
  • 275
  • 473
  • I would count implicit conversion as a disadvantage in some situations (but then again I would try an prevent it in the first place by having an explicit constructor). If your type is another numerical type then implicit conversion is probably OK but other types I would have to consider on a case by case basis. – Martin York Apr 25 '12 at 14:32
  • @LokiAstari: Agreed that implicit conversions should be avoided, but if there is one, it does not make sense to behave differently on the left and right hand sides of an operation, that is, the issue with implicit conversions exists in both cases, but in one of them it is consistent. As I tried to say, having a free function will never hurt and might help in some cases (where implicit conversions are desired). – David Rodríguez - dribeas Apr 25 '12 at 14:50
  • Some of the stuff I could find was talking about symmetry and class encapsulation, but I guess implicit conversions is an actual practical advantage. Only heard about non-member operator overloading recently on SO and was rather surprised that the member operator< was dismissed as "bad practice" and something that you "shouldn't do", especially since it was the only way I knew of and had used it for years all over my code. Good to know it's not as Bad as I feared. – smocking Apr 25 '12 at 16:31