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.