class StrangeFunctor
{
public:
StrangeFunctor(int (*comp)(string, string))
{
this->comp = comp;
}
int operator()(string str1, string str2)
{
return –comp(str1, str2);
}
private:
int (*comp)(string, string);
}
I was just curious as what the above code actually did. Assuming the functor was properly initialized and given to a sorting function for comparison purpose, my hunch is that it reverses the order of the passed argument, but I'm not sure if that is correct and why the would be correct.
std::sort
. It would indeed work the way you describe for the sorting function that expects the comparison to return a negative value for less-than, zero for equals and positive for greater-than (likeqsort
, but you won't be able to pass a function object toqsort
).comp
returnsINT_MIN
, then-
will cause overflow (assuming normal two's complement implementation ofint
).