Function objects vs. function pointers

P

Protoman

Which is more efficient for stuff like callbacks, selecting a funct
from a list, and other stuff, function objects or function pointers?
Thanks!!!
 
A

Alf P. Steinbach

* Protoman:
Which is more efficient for stuff like callbacks, selecting a funct
from a list, and other stuff, function objects or function pointers?
Thanks!!!

The red one, obviously, at line 561.

If you're interested in a serious answer you'll have to do the work
yourself before asking, so then there's not much point in asking:

* Define efficiency in a measurable way (e.g. running time,
memory consumption, development time, development cost, ...).

* Define the cases you're interested in.

* Define the platforms you're interested in.

* Create and/or identify test programs.

* Measure.

Anything else is pure speculation about hypothetical cases.

But first of all, keep at heart (or behind your ear) that

Premature optimization is the root of all evil.

Optimization is always a trade-off, meaning you trade away something,
and usually that something is maintainability and correctness.
 
P

Protoman

For efficiency , I mean memory and speed, for cases, I mean callbacks
and selecting from a list, platforms I mean workstations and servers.
Here's a test program:

#include <iostream>
#include <cstdlib>
using namespace std;

class Functor
{
public:
void operator()()const{cout << "Hello!!!" << endl;}
};

void(*pfn)();

void fn(){cout << "Hello!!!" << endl;}

void call(void(*pfn)()){pfn();}

void callback(const Functor& fn){fn();}

int main()
{
pfn=fn;
call(pfn);
callback(Functor());
system("PAUSE");
return 0;
}

Any suggestions? Thanks!!!
 
Y

yuvalif

Well, I'm just speculating here, but if you are writing in C++, why use
a C-style callback mechanism?
You can define an interface class, with the callbacks as pure virtual
member functions, and let the others implement them in derived classes.
You won't need function pointers nor functors in that way. I'm not sure
about its efficiency, but the code will make much more sense in that
way.

Yuval.
 
M

Mateusz Loskot

Protoman said:
Which is more efficient for stuff like callbacks, selecting a funct
from a list, and other stuff, function objects or function pointers?

IMHO functors vel function objects.
The main reason is that functors can be much better optimized by
compiler. Compiler can inline it what is almost impossible with
function pointer. But the main reason I choose functor is its better
genericity, functor can be modified without affecting users and can be
used as a temlate parameter what increases algorithms composition
capabilities. Another, functors fit well to OOP and design patterns.
They are abstractions easier to understand in this case.

Cheers
 
R

Rolf Magnus

Protoman said:
For efficiency , I mean memory and speed, for cases, I mean callbacks
and selecting from a list, platforms I mean workstations and servers.
Here's a test program:

#include <iostream>
#include <cstdlib>
using namespace std;

class Functor
{
public:
void operator()()const{cout << "Hello!!!" << endl;}
};

void(*pfn)();

void fn(){cout << "Hello!!!" << endl;}

void call(void(*pfn)()){pfn();}

void callback(const Functor& fn){fn();}

int main()
{
pfn=fn;
call(pfn);
callback(Functor());
system("PAUSE");
return 0;
}

You're comparing apples and oranges. One important aspect of function
pointers is that you can change the function they point to at runtime, but
with your callback() function, the function that is called is fixed at
compile time. Of course this gives the compiler a better opportunity to
optimize, but you can't dynamically change anyhting.
 
M

Marcus Kwok

Protoman said:
Oh, and in:


how can the compiler call the Functor if it's unnamed?

If you look at one of the lines you snipped:

What is happening is that the temporary Functor object gets bound to the
const reference "fn" in the callback() function. So, it does have a
name in that context, and that name is "fn".
 
P

Protoman

Marcus said:
If you look at one of the lines you snipped:


What is happening is that the temporary Functor object gets bound to the
const reference "fn" in the callback() function. So, it does have a
name in that context, and that name is "fn".

So that what happens. And, I've been wondering, what does this
declaration mean:

void fn(void(*)(int&,int&),int&,int&);

And exactly why do you use callback functions?
 
R

roberth+news

| So that what happens. And, I've been wondering, what does this
| declaration mean:
|
| void fn(void(*)(int&,int&),int&,int&);

Obviously, fn is a function. It is declared on the form

type name(arg-list)
with type = void, and arg-list consists of
1: void(*)(int&,int&)
2: int&
3: int&

The two last arguments are the easiest ones. They must be references to
ints. The first type should be memorized:

void (*)(int&, int&)

means a pointer (*) to a function (int&, int&) returning void. One
could also have used the form:

void (*f)(int&, int&)

This form is declaring the expression (*f) to be a function taking
(int&, int&), and returning void, and thus f must be a pointer to this.
Because those smart ones, who created the standard, found it was smart
to allow someone call a function pointer. one can call the function
pointed to by f, with the expression f(a, b), but you can still write
(*f)(a, b).

| And exactly why do you use callback functions?

Callbacks are very practical in some contexts. A common application is
GUI programming, where callbacks is used for `call this function if and
when the user presses this button'. This allows for flexible GUI
libraries. The standard library uses callbacks too. The routine
for_each, for example, calls a function for every argument in a
sequence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top