Sign in to follow this  
Concentrate

Question about a method.

Recommended Posts

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 this post


Link to post
Share on other sites
Quote:
Original post by Concentrate
So 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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this