Skip to main content
added 279 characters in body
Source Link
Donotalo
  • 12.9k
  • 25
  • 87
  • 122

Notice that, compare_class is a class. So it can be declared, passed as function parameter the way you use other classes.

Secondly, compare_class implementes operator(). This allows you to do the following:

compare_class obj;
obj(1, 2);

Now, the second statement in the above code fragment seems like a function call! So in a sense, any object of a class that implements operator() can be use like a function.

That's the point of function objects. Besides, it has other advantages over function pointers which you will find in the same link you've given in your post.

EDIT

Inside sort_ints(), functor is expected to do something like the following:

for (int i = 1; i < num_items; i++)
    if (c(begin_items[i-1], begin_items[i]))
    {
        // begin_items[i-1] is less than begin_items[i], do stuff
    }

Notice that, compare_class is a class. So it can be declared, passed as function parameter the way you use other classes.

Secondly, compare_class implementes operator(). This allows you to do the following:

compare_class obj;
obj(1, 2);

Now, the second statement in the above code fragment seems like a function call! So in a sense, any object of a class that implements operator() can be use like a function.

That's the point of function objects. Besides, it has other advantages over function pointers which you will find in the same link you've given in your post.

Notice that, compare_class is a class. So it can be declared, passed as function parameter the way you use other classes.

Secondly, compare_class implementes operator(). This allows you to do the following:

compare_class obj;
obj(1, 2);

Now, the second statement in the above code fragment seems like a function call! So in a sense, any object of a class that implements operator() can be use like a function.

That's the point of function objects. Besides, it has other advantages over function pointers which you will find in the same link you've given in your post.

EDIT

Inside sort_ints(), functor is expected to do something like the following:

for (int i = 1; i < num_items; i++)
    if (c(begin_items[i-1], begin_items[i]))
    {
        // begin_items[i-1] is less than begin_items[i], do stuff
    }
Source Link
Donotalo
  • 12.9k
  • 25
  • 87
  • 122

Notice that, compare_class is a class. So it can be declared, passed as function parameter the way you use other classes.

Secondly, compare_class implementes operator(). This allows you to do the following:

compare_class obj;
obj(1, 2);

Now, the second statement in the above code fragment seems like a function call! So in a sense, any object of a class that implements operator() can be use like a function.

That's the point of function objects. Besides, it has other advantages over function pointers which you will find in the same link you've given in your post.