Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

71%
+3 −0
Q&A Are there practical reasons for designing a method-only class/object?

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 ...

posted 3y ago by Lundin‭  ·  edited 3y ago by Lundin‭

Answer
#2: Post edited by user avatar Lundin‭ · 2021-09-09T09:05:17Z (over 3 years ago)
  • 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 by user avatar Lundin‭ · 2021-09-09T09:04:32Z (over 3 years ago)
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.