Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie question about operator() and functors in general

207 views
Skip to first unread message

Murphy Pope

unread,
Oct 9, 2004, 6:38:25 AM10/9/04
to
I'm still trying to wrap my head around the usefulness
of operator(). I see it used everywhere, but I don't
understand why, when I design a class, I would want to
define an operator(). Why wouldn't I just define a member
function named doYourJob() that does the same thing, probably
in a more self-documenting way?

Does operator() allow me to pass an object (reference) to
any function that expects a function pointer? Are functors
then interchangable with functions pointers? Is functor the
right word to use for any object that defines a public operator()?

TIA.

-- Murphy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Maciej Sobczak

unread,
Oct 10, 2004, 5:41:02 AM10/10/04
to
Hi,

Murphy Pope wrote:
> I'm still trying to wrap my head around the usefulness
> of operator(). I see it used everywhere, but I don't
> understand why, when I design a class, I would want to
> define an operator(). Why wouldn't I just define a member
> function named doYourJob() that does the same thing, probably
> in a more self-documenting way?

operator() is a *common* name for doYourJob(), do_it(), doIt(),
compute(), proceed(), run(), fire(), invoke(), etc.
This means that many programmers may write compatible code without
actually communicating to agree on the name (and that's usually most
difficult...).

Moreover, operator() allows the functor to share the syntax with regular
functions and function pointers.

> Does operator() allow me to pass an object (reference) to
> any function that expects a function pointer? Are functors
> then interchangable with functions pointers?

No, they are not interchangeable in the sense that you cannot pass a
functor where a function pointer is expected. But the fact that they can
share the same syntax for calling means that you can write generic code
against both:

template <class Functor>
void foo(Functor f)
{
// bla bla
f();
}

and later call foo with whatever you want: functions, function pointers,
functors.

> Is functor the
> right word to use for any object that defines a public operator()?

Many people use it that way. You may also find people differentiating
between a *class* that defines operator() and an *object* (an instance)
of such a class, calling them functor and function object, respectively.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

Victor Bazarov

unread,
Oct 10, 2004, 6:12:09 PM10/10/04
to
"Murphy Pope" <pope_...@hotmail.com> wrote...

> I'm still trying to wrap my head around the usefulness
> of operator(). I see it used everywhere, but I don't
> understand why, when I design a class, I would want to
> define an operator(). Why wouldn't I just define a member
> function named doYourJob() that does the same thing, probably
> in a more self-documenting way?

Because if you plan on using an object of your class in a generic
function (like 'std::for_each' or 'std::transform'), they cannot
figure out that you planned to use 'doYourJob' and instead count
on the existence of 'operator()'.

> Does operator() allow me to pass an object (reference) to
> any function that expects a function pointer?

No.

> Are functors
> then interchangable with functions pointers?

No.

Functors are used in templates, and only there objects of class
type that define operator() are interchangeable with function
pointers.

Get a book on generic programming using standard C++ library and
you will get plenty of examples.

> Is functor the
> right word to use for any object that defines a public operator()?

A functor is anything that can be used as a function. Therefore
a function is a functor too.

Victor

jakacki

unread,
Oct 10, 2004, 6:23:55 PM10/10/04
to
> I'm still trying to wrap my head around the usefulness of
> operator(). I see it used everywhere, but I don't
> understand why, when I design a class, I would want to
> define an operator(). Why wouldn't I just define a
> member function named doYourJob() that does the same
> thing, probably in a more self-documenting way?

Because some generic code requires your class to define
operator(). Consider

http://www.sgi.com/tech/stl/Map.html

user-supplied class 'ltstr' has to define operator().

> Does operator() allow me to pass an object (reference) to
> any function that expects a function pointer?

No. If a function signature specifies function pointer you
cannot pass functor.

> Are functors then interchangable with functions pointers?

Yes, as arguments of function templates that do not specify
particular type. Function template

template <class Fun>
void g(Fun f)
{
f(0);
};

will accept function pointer, function reference or
functor.

> Is
> functor the right word to use for any object that defines
> a public operator()?

The term "functor" does not appear in C++ Standard. Standard
uses the term "function object" [20.3p1]:

Function objects are objects with an operator() defined.

Nicolai Josuttis in "The C++ Standard Library" equates
"function object" and "functor".

BR
Grzegorz

--
Free C++ frontend library: http://opencxx.sourceforge.net
China from the inside: http://www.staryhutong.com
Myself: http://www.dziupla.net/gj/cv

Murphy Pope

unread,
Oct 10, 2004, 7:13:36 PM10/10/04
to
> operator() is a *common* name for doYourJob(), do_it(), doIt(),
> compute(), proceed(), run(), fire(), invoke(), etc.
> This means that many programmers may write compatible code without
> actually communicating to agree on the name (and that's usually most
> difficult...).

Ok, that makes sense.

> > Does operator() allow me to pass an object (reference) to
> > any function that expects a function pointer? Are functors
> > then interchangable with functions pointers?
>
> No, they are not interchangeable in the sense that you cannot pass a
> functor where a function pointer is expected. But the fact that they can
> share the same syntax for calling means that you can write generic code
> against both:

Aha... now I see. When you throw templates into the mix, you *can*
use a functor in place of a function pointer. Now it all makes sense.
Thanks so much.

-- Murphy

0 new messages