Function objects vs. function pointers

Discussion in 'C++' started by Protoman, Dec 7, 2005.

  1. Protoman

    Protoman Guest

    Which is more efficient for stuff like callbacks, selecting a funct
    from a list, and other stuff, function objects or function pointers?
    Thanks!!!
    Protoman, Dec 7, 2005
    #1
    1. Advertising

  2. * 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?
    Alf P. Steinbach, Dec 7, 2005
    #2
    1. Advertising

  3. Protoman

    Protoman Guest

    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!!!
    Protoman, Dec 7, 2005
    #3
  4. Protoman

    Guest

    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, 2005
    #4
  5. 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
    Mateusz Loskot, Dec 7, 2005
    #5
  6. Protoman

    Rolf Magnus Guest

    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.
    Rolf Magnus, Dec 7, 2005
    #6
  7. Protoman

    Howard Guest

    "Mateusz Loskot" <> wrote in message
    news:...

    >
    > IMHO functors vel function objects.


    What's "vel"???

    -Howard
    Howard, Dec 7, 2005
    #7
  8. Howard wrote:
    > "Mateusz Loskot" <> wrote in message
    > news:...
    >
    > >
    > > 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
    Mateusz Loskot, Dec 7, 2005
    #8
  9. Protoman

    Protoman Guest

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


    how can the compiler call the Functor if it's unnamed?
    Protoman, Dec 8, 2005
    #9
  10. Protoman

    Marcus Kwok Guest

    Protoman <> 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
    Marcus Kwok, Dec 8, 2005
    #10
  11. Protoman

    Protoman Guest

    Marcus Kwok wrote:
    > Protoman <> 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?
    Protoman, Dec 9, 2005
    #11
  12. Protoman

    Guest

    Protoman <> 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, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    643
    llewelly
    Sep 16, 2003
  2. Brian
    Replies:
    2
    Views:
    329
    Brian
    Oct 31, 2003
  3. Marc Thrun
    Replies:
    15
    Views:
    857
    Tim Rentsch
    Oct 4, 2005
  4. 7stud
    Replies:
    11
    Views:
    684
    Dennis Lee Bieber
    Mar 20, 2007
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    669
Loading...

Share This Page