473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function objects vs. function pointers

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

Dec 7 '05 #1
11 2592
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 7 '05 #2
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!!!

Dec 7 '05 #3
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.

Dec 7 '05 #4
Protoman wrote:
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
--
Mateusz Loskot
http://mateusz.loskot.net

Dec 7 '05 #5
Protoman wrote:
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.

Dec 7 '05 #6

"Mateusz Loskot" <ma*****@loskot.net> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

IMHO functors vel function objects.


What's "vel"???

-Howard

Dec 7 '05 #7
Howard wrote:
"Mateusz Loskot" <ma*****@loskot.net> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

IMHO functors vel function objects.


What's "vel"???


Excuse me, I should write "that is" or "known as" or "as known as"
instead of "vel".
Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

Dec 7 '05 #8

Oh, and in:
Protoman wrote:
callback(Functor());


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

Dec 8 '05 #9
Protoman <Pr**********@gmail.com> wrote:

Oh, and in:
Protoman wrote:
callback(Functor());


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


If you look at one of the lines you snipped:
void callback(const Functor& fn){fn();}


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".

--
Marcus Kwok
Dec 8 '05 #10

Marcus Kwok wrote:
Protoman <Pr**********@gmail.com> wrote:

Oh, and in:
Protoman wrote:
callback(Functor());


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


If you look at one of the lines you snipped:
void callback(const Functor& fn){fn();}


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".

--
Marcus Kwok


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?

Dec 9 '05 #11
Protoman <Pr**********@gmail.com> wrote:
| 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.
--
Robert Bauck Hamar
Dec 9 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brian | last post by:
Hi all, I am implementing an object that is currently using function pointers for callbacks to let me do something on assignment: template <class T> class MyClass{ private: typedef T& (*...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
2
by: Jack | last post by:
I have a chunk of code that loads a few dozen function pointers into global variables. I'm concerned with unused memory consumption. What if the client only needs to use one or two functions? Then...
5
by: wongjoekmeu | last post by:
Hiya all, I am trying to use function pointers. I know that usually if you use a pointer to an object, you have to release the memory at the end by calling the delete keyword. I was wondering if...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.