|
-
May 28th, 2010, 12:46 PM
#15
Re: Functor Tutorial
 Originally Posted by Peter_APIIT
What is the advantage of functor organization in the case of a class is derived from unary_function ?
In my opinion, functor not just "is nothing more than a wrapper to "operator<". " but normally it is designed as a predicate for looping construct.
Why you say functor is a wrapper for operator< ?
Please comment.
If you can borrow or get a copy of effective STL by Scott Meyers he does a good job of explaining why. A function pointer has to be copied by value. Using a functor can result in more effecient inline code. From my point of view as an OO programmer a functor seems more intuitive to me. Also functors are classes/structs and therefore you can inherit from other classes within the STL that makes them more adaptable. Take a look at the following example. Notice that the functor inherits from the unary_function class. This is critical if you want to be able to take advantage of things like not1. Instead of writing two functors you write one and then use not1 to make it do the opposite.
http://cplusplus.com/reference/std/functional/not1/
In my opinion a functor is a much more powerful tool then a global or static predicate function.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|