Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Many languages support the concept of functors or function objects which are classes only containing a method/member function. Most notably C++ STL was designed around this - whenever you declare ...
Answer
#2: Post edited
Many languages support the concept of _functors_ or _function objects_ which are classes only containing a method.- Most notably C++ STL was designed around this - whenever you declare a C++ standard container class object, you have the optional argument defining how that container is sorted. How can specify default sorting with `std::less` etc or you can give your own custom sorting functor.
- If you look at how `std::less` was implemented [here](https://www.cplusplus.com/reference/functional/less/), you'll see that it contains nothing but a public member function. Which in turn uses the `<` operator for the class to sort, so that one needs to be present. Similarly, you can pass `std::less` to `std::sort()` and similar functions.
- This concept exists in many other languages too. For example C with no explicit OO support, has functions `bsearch` and `qsort` which also use the very same concept. Only they take a function pointer instead of an class/object. _Callback functions_ in general are very similar to functors - behaviour templates passed to an API which is then later called internally by that library.
- Many languages support the concept of _functors_ or _function objects_ which are classes only containing a method/member function.
- Most notably C++ STL was designed around this - whenever you declare a C++ standard container class object, you have the optional argument defining how that container is sorted. How can specify default sorting with `std::less` etc or you can give your own custom sorting functor.
- If you look at how `std::less` was implemented [here](https://www.cplusplus.com/reference/functional/less/), you'll see that it contains nothing but a public member function. Which in turn uses the `<` operator for the class to sort, so that one needs to be present. Similarly, you can pass `std::less` to `std::sort()` and similar functions.
- This concept exists in many other languages too. For example C with no explicit OO support, has functions `bsearch` and `qsort` which also use the very same concept. Only they take a function pointer instead of an class/object. _Callback functions_ in general are very similar to functors - behaviour templates passed to an API which is then later called internally by that library.
#1: Initial revision
Many languages support the concept of _functors_ or _function objects_ which are classes only containing a method. Most notably C++ STL was designed around this - whenever you declare a C++ standard container class object, you have the optional argument defining how that container is sorted. How can specify default sorting with `std::less` etc or you can give your own custom sorting functor. If you look at how `std::less` was implemented [here](https://www.cplusplus.com/reference/functional/less/), you'll see that it contains nothing but a public member function. Which in turn uses the `<` operator for the class to sort, so that one needs to be present. Similarly, you can pass `std::less` to `std::sort()` and similar functions. This concept exists in many other languages too. For example C with no explicit OO support, has functions `bsearch` and `qsort` which also use the very same concept. Only they take a function pointer instead of an class/object. _Callback functions_ in general are very similar to functors - behaviour templates passed to an API which is then later called internally by that library.