POV-Ray : Newsgroups : povray.off-topic : C++ questions : Re: C++ questions Server Time
7 Sep 2024 13:21:30 EDT (-0400)
  Re: C++ questions  
From: Warp
Date: 26 Sep 2008 14:22:48
Message: <48dd2877@news.povray.org>
Orchid XP v8 <voi### [at] devnull> 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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.