Review: functor / function object

Function object, also called functor, functional, or functionoid,is a computer programming construct allowing an object to be invoked or called as though it were an ordinary function, usually with the same syntax.

Several notes for it:

1. In C, there is only function pointer; in C#, there is only delegate; while in C++, by overriding operator() of a class/struct, its instance can be used just like a function pointer:

 functor_class Y;
int result = Y( a, b );
2. Its advantages:
  1) performance. Due to it's actually called like Y(a, b) so the function can be known at compile time, so can be set inlined, which will improve performance.
2) can maintain state. It's actually a object passed into a container like std::map for comparison for sorting, so it can have its state. It can't be achieved by function pointer
or delegate.

 

posted @ 2010-12-22 17:38  能巴  阅读(196)  评论(0编辑  收藏  举报