Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top