Function objects vs. function pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Protoman

    #1

    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!!!

  • Alf P. Steinbach

    #2
    Re: Function objects vs. function pointers

    * Protoman:[color=blue]
    > Which is more efficient for stuff like callbacks, selecting a funct
    > from a list, and other stuff, function objects or function pointers?
    > Thanks!!![/color]

    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?

    Comment

    • Protoman

      #3
      Re: Function objects vs. function pointers

      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()()con st{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(Functo r());
      system("PAUSE") ;
      return 0;
      }

      Any suggestions? Thanks!!!

      Comment

      • yuvalif@gmail.com

        #4
        Re: Function objects vs. function pointers

        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.

        Comment

        • Mateusz Loskot

          #5
          Re: Function objects vs. function pointers

          Protoman wrote:[color=blue]
          > Which is more efficient for stuff like callbacks, selecting a funct
          > from a list, and other stuff, function objects or function pointers?[/color]

          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


          Comment

          • Rolf Magnus

            #6
            Re: Function objects vs. function pointers

            Protoman wrote:
            [color=blue]
            > 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()()con st{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(Functo r());
            > system("PAUSE") ;
            > return 0;
            > }[/color]

            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.

            Comment

            • Howard

              #7
              Re: Function objects vs. function pointers


              "Mateusz Loskot" <mateusz@loskot .net> wrote in message
              news:1133950322 .648055.56140@o 13g2000cwo.goog legroups.com...
              [color=blue]
              >
              > IMHO functors vel function objects.[/color]

              What's "vel"???

              -Howard



              Comment

              • Mateusz Loskot

                #8
                Re: Function objects vs. function pointers

                Howard wrote:[color=blue]
                > "Mateusz Loskot" <mateusz@loskot .net> wrote in message
                > news:1133950322 .648055.56140@o 13g2000cwo.goog legroups.com...
                >[color=green]
                > >
                > > IMHO functors vel function objects.[/color]
                >
                > What's "vel"???[/color]

                Excuse me, I should write "that is" or "known as" or "as known as"
                instead of "vel".


                Cheers
                --
                Mateusz Loskot


                Comment

                • Protoman

                  #9
                  Re: Function objects vs. function pointers


                  Oh, and in:
                  Protoman wrote:[color=blue]
                  > callback(Functo r());[/color]

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

                  Comment

                  • Marcus Kwok

                    #10
                    Re: Function objects vs. function pointers

                    Protoman <Protoman2050@g mail.com> wrote:[color=blue]
                    >
                    > Oh, and in:
                    > Protoman wrote:[color=green]
                    >> callback(Functo r());[/color]
                    >
                    > how can the compiler call the Functor if it's unnamed?[/color]

                    If you look at one of the lines you snipped:
                    [color=blue][color=green]
                    >> void callback(const Functor& fn){fn();}[/color][/color]

                    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

                    Comment

                    • Protoman

                      #11
                      Re: Function objects vs. function pointers


                      Marcus Kwok wrote:[color=blue]
                      > Protoman <Protoman2050@g mail.com> wrote:[color=green]
                      > >
                      > > Oh, and in:
                      > > Protoman wrote:[color=darkred]
                      > >> callback(Functo r());[/color]
                      > >
                      > > how can the compiler call the Functor if it's unnamed?[/color]
                      >
                      > If you look at one of the lines you snipped:
                      >[color=green][color=darkred]
                      > >> void callback(const Functor& fn){fn();}[/color][/color]
                      >
                      > 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[/color]

                      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?

                      Comment

                      • roberth+news@ifi.uio.no

                        #12
                        Re: Function objects vs. function pointers

                        Protoman <Protoman2050@g mail.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&,in t&)
                        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

                        Comment

                        Working...