Hi All,
I have two questions related to function objects and function pointers,
Question : 1
When I read the different uses sort
algorithm of STL, I see that the third parameter can be a function objects, below is an example
class State {
public:
//...
int population() const;
float aveTempF() const;
//...
};
struct PopLess : public std::binary_function<State,State,bool> {
bool operator ()( const State &a, const State &b ) const
{ return popLess( a, b ); }
};
sort( union, union+50, PopLess() );
Question :
Now, How does the statement, sort(union, union+50,PopLess())
work? PopLess()
must be resolved into something like PopLess tempObject.operator()
which would be same as executing the operator ()
function on a temporary object. I see this as, passing the return value of overloaded operation i.e bool
(as in my example) to sort
algorithm.
So then, How does sort function resolve the third parameter in this case?
Question : 2
Question
Do we derive any particular advantage of using function objects versus function pointer? If we use below function pointer will it derive any disavantage?
inline bool popLess( const State &a, const State &b )
{ return a.population() < b.population(); }
std::sort( union, union+50, popLess ); // sort by population
PS : Both the above references(including example) are from book "C++ Common Knowledge: Essential Intermediate Programming" by "Stephen C. Dewhurst".
I was unable to decode the topic content, thus have posted for help.
Thanks in advance for your help.