C++ operator overloading, understanding the Google style guide
c++, syntax
Solution
A functor type would be more like this:
struct CatalogueItemLessThan
{
bool operator()(const CatalogueItem &a, const CatalogueItem &b)
{
}
};
Then the usage would look like this:
list<CatalogueItem> my_list;
// this is just me playing around
CatalogueItem items[2];
items[0] = CatalogueItem(4, string("box"), string("it's a box"));
items[1] = CatalogueItem(3, string("cat"), string("it's a cat"));
my_list.push_back(items[0]);
my_list.push_back(items[1]);
my_list.sort(CatalogueItemLessThan());
The main advantage of this, is that is allows you to decouple sorting from the object itself. You can now provide as many types of sorting as you want, and use them in different places. (For example, string can be sorted in lexical order, or case-insensitively, or "naturally".
The advantage of using a functor as opposed to a loose function is that you can pass parameters into the comparison to modify how the functor should behave.
In general, the Google style-guide is not really the best style guide out there (IMHO especially their taking exception to exceptions, but that's another discussion). If an object has an obvious sorting order, I often add in a default `operator<`. If later, there are extra sort orders I want to add, then I add in loose functions. If at a later time, I need to add parameters to the sort order, then I make them into functors. There's no sense in adding in complexity before it's needed.
Problem
I am following a book to learn C++ (come from a python background). I've written this, which works: ``` class CatalogueItem { public: CatalogueItem(); CatalogueItem(int item_code, const string &name, const string &description); ~CatalogueItem() {}; bool operator< (const CatalogueItem &other) const; ... private: ... }; ... list<CatalogueItem> my_list; // this is just me playing around CatalogueItem items[2]; items[0] = CatalogueItem(4, string("box"), string("it's a box")); items[1] = CatalogueItem(3, string("cat"), string("it's a cat")); my_list.push_back(items[0]); my_list.push_back(items[1]); my_list.sort(); ``` The part I'm trying out is using the operator < to allow the list to sort itsself. This all seems good, but http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Operator_Overloading seems to suggest avoiding doing this, which is exactly what the book says to do! ("In particular, do not overload operator== or operator< just so that your class can be used as a key in an STL container; instead, you should create equality and comparison functor types when declaring the container.") I understand "create equality and comparison functor types" to mean creating comparison functions, like the below one: ``` bool my_comparison_function(const CatalogueItem &a, const CatalogueItem &b) { // my comparison code here } ``` Is that what the style guide is referring to? Does anyone have an option as to which method is more "correct"? J