|
![](/i/fill.gif) |
Orchid XP v8 <voi### [at] dev null> wrote:
> Warp wrote:
> > Isn't this usually handled with callback interfaces (in OO languages)
> > rather than function pointers?
> Speaking of which... apparently you can overload "()", thus creating an
> object that looks like a function... How far-out is that? o_O
So-called functors are a common idiom in C++ template programming.
For example many STL data containers and algorithms accept functors
as parameters.
A functor is something which acts like a function. In other words, it
can be called like a function can be called, and it will return a value
like a function returns a value.
Obviously a regular function is a valid functor (and a common way to
pass functors to the STL).
Another way of creating a functor is, as you point out, to create a
struct or class which has the operator() overloaded. This way an instance
of that class can be used exactly like it was a function.
The advantage of the latter method is that the struct/class can have
member variables (and member functions) which the operator() can use.
This way you can have different values for those member variables for
different instances of the functor. (If I'm not completely mistaken,
this starts get closer to so-called closures.)
In the next C++ standard there will be a third type of functor: Lambda
functions.
> I'm almost tempted to try just to see if I can implement curried
> functions with it in C++. You know, just to be totally perverse...
C++ might not be the best language for that... ;)
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |