Question

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.

Était-ce utile?

La solution

This functor takes in a function pointer and then flips the sign on that method's return value.

return –comp(str1, str2); 

If used with sorting like you said, it would invert the order of what was being sorted, given by the original function pointer.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top