## Recommended Posts

Concentrate    181
This is not for a particular class, but in general, but I'll use a class as an example. Say we have a number class like so :
class Number{
//...
public:
//operators
bool operator>( const Number& ) const;
bool operator<( const Number& ) const;
bool operator>=( const Number&, ) const;
bool operator<=( const Number&, ) const;
}

The boolean operators, all can be condensed to a single function namely like so:
class Number{
public:
int compare(const Number& otherNumber)const;
}

So my question would be, which method would be better?

##### Share on other sites
Drew_Benton    1861
Quote:
 Original post by ConcentrateSo my question would be, which method would be better?

There are two parts to this question:

1.) It's more of a matter of syntax preference and your API design rather than better/worse.

For example, which of these operations do you like more? (rhetorical question)
Number n1;Number n2;if(n1 < n2)   ; // ...if(n1.Compare(n2) < 0)   ; // ...
It's purely preference. Some people might like having a more natural style of comparison via operation while others might want to use a function instead. You could offer both to cater to all users.

2a.) If you are writing objects that will be used in the Standard C++ Library containers, you will eventually need to define certain operators for functionality. If you already have such operators in place, you save a lot of time and work later on for you or your end users.

Simple example:
#include <vector>#include <map>#include <algorithm>class MyObject{private:public:	MyObject()	{	}	~MyObject()	{	}	// Needed for find	bool operator==(const MyObject & rhs) const	{		return true;	}	// Needed for map	bool operator<(const MyObject & rhs) const	{		return true;	}};int main(int argc, char * argvp[]){	std::vector<MyObject> objs;	MyObject fnd;	std::find(objs.begin(), objs.end(), fnd);	std::map<MyObject, int> objs2;	objs2[fnd] = 0;	return EXIT_SUCCESS;}

2b.) If you want to expose your class via scripting, having one function is more friendly on both sides rather than having an operator that might not be supported directly.

For example, in Lua, having the function to expose will result in a lot less work down the line rather than having to write additional functions for each operator. So it's a time spent now vs. time spent later if you ever go that route.

So, my recommendation would be to offer both and not only one or the other.

##### Share on other sites
Concentrate    181

##### Share on other sites
Zahlman    1682
You can, of course, write a private .compare() and use it to implement the other operator overloads. But that might not make things any easier for you.

But you don't really need to implement all six comparisons anyway (unless there's something special about your class that lets you optimize things by doing it that way). The usual idiom is to write only operator < and operator ==, and rely on a global template function that implements the others in terms of those.