How To Create Functors and Function Objects |
Written by Mike James | ||||
Monday, 17 June 2024 | ||||
Page 3 of 3
Passing function objectsAs a function object is just a standard object you can pass it as a parameter in another method. For example: class MyClass Notice that MyMethod accepts a parameter of type MyFunctor and when supplied simply calls it as a function. For example: MyFunctor MyFunction(2); Comparing this to using a function pointer you can see that not only is it simpler, it fits in much better with the object-oriented idea. However, it has to be admitted that you can't do the same fancy tricks that you can achieve with a pointer - for example changing the function the pointer references at run time. This said function objects are heavily used in the STL to pass functions - often predicates - into methods. The complete program is: class MyFunctor public: int operator()(int i) class MyClass int main() An example of closureReturning for a moment to the idea that function object allow C++ programmers to implement closure we can see how this works with a simple example. One of the common uses of closure is to allow a callback function to access variables that are defined within its original context. For example suppose you have to supply a callback function to an object that searches an index for a target. You could implement a cut off value that aborted the search when the number of hits reaches a limit. If C++ implemented closure you could do this as follows. First define the callback as a function object: int operator () (Results Rs) where it is assumed that the callback accepts an object of type Results that the search object uses to communicate the progress of the search. Notice that the signature of the callback function is predefined by the object using the callback - you can't change it. This is all fairly simply except for where the HitLimit variable comes from. Notice that you can't include HitLimit as a parameter in the callback function because it has to fit the specification supplied for the callback. If C++ supported closure directly we could just create HitLimit as a local variable in the same scope as the function - closure would then make the variable available within the function even if the local context had been destroyed. That is, if C++ supported closure you could just write: int HitLimit=5; Yes this would be enough to make HitLimit accessible within MyFunction if closure was in effect. However we all know that C++ doesn't implement closure so what you have to do is simply add a suitable property to the function object or use the constructor to initialize a private variable: class MyFunctor Then you can use the callback as: MyFunctor MyFunction(10); Using a property has the advantage the it makes clear what is being set e.g. MyFunction.HitLimit=10; There are lots of very clever and sophisticated uses of function objects. The sorts of things you need to keep in mind are that because a function object is just an object it can take part in inheritance, polymorphism and you can use templates to create generic functors. However as with most of C++ just because it is possible doesn't means it is a good idea. Try and keep things simple. To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info |
||||
Last Updated ( Monday, 17 June 2024 ) |