function pointer

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

    function pointer

    Hello -
    I am using a library that takes a function pointer as an argument. Is the code below not possible?


    int library_func(vo id (*func)(int, short, void *));


    I am trying to do this...

    class Test {
    public:
    Test();
    void a(int, short, void *);
    void loop(void);
    };

    Test::Test()
    {
    }

    void Test::a(int a, short b, void *c)
    {
    }

    void Server::loop(vo id)
    {
    void (*func)(int, short, void *) = a;

    library_functio n(func);
    }

    int main(void)
    {
    Test s;
    s.loop();

    return (0);
    }
  • Ron Natalie

    #2
    Re: function pointer


    "David Hill" <david@wmol.com > wrote in message news:2003062414 0611.71b47dfa.d avid@wmol.com.. .[color=blue]
    > Hello -
    > I am using a library that takes a function pointer as an argument. Is the code below not possible?
    >
    > void (*func)(int, short, void *) = a;
    >[/color]

    It is not possible. Member functions are different than regular functions.
    You can't convert between the two. What object is the member function
    going to be invoked on if you only have the member address?

    If you can't change the interface to the library, you're going to have to wrap the
    member function call in an ordinary function. You can squirrel away the pointer
    to member and "this" pointer in that extra void* operand I suspect.


    Comment

    • Jeremy

      #3
      Re: function pointer

      "Ron Natalie" <ron@sensor.com > wrote in message
      news:Q4ydnVb9Vs QhCWWjXTWQkQ@gi ganews.com...[color=blue]
      >
      > "David Hill" <david@wmol.com > wrote in message[/color]
      news:2003062414 0611.71b47dfa.d avid@wmol.com.. .[color=blue][color=green]
      > > Hello -
      > > I am using a library that takes a function pointer as an argument. Is[/color][/color]
      the code below not possible?[color=blue][color=green]
      > >
      > > void (*func)(int, short, void *) = a;
      > >[/color]
      >
      > It is not possible. Member functions are different than regular[/color]
      functions.[color=blue]
      > You can't convert between the two. What object is the member function
      > going to be invoked on if you only have the member address?
      >
      > If you can't change the interface to the library, you're going to have to[/color]
      wrap the[color=blue]
      > member function call in an ordinary function. You can squirrel away the[/color]
      pointer[color=blue]
      > to member and "this" pointer in that extra void* operand I suspect.
      >[/color]

      What does that mean (You can squirrel away the pointer to member and "this"
      pointer in that extra void* operand I suspect)?
      (i'm a newb, sorry)


      Comment

      • Tony Di Croce

        #4
        Re: function pointer


        Declare a member function of you're object as static. This will keep it from
        getting a "this" pointer, and you'll be able to point a C style function
        pointer at it. Pass this to you're library. Additionally (and this is the
        squirling away part), most of the time when you're doing this, the call that
        tells the library where the function to callback is also includes a void*
        that you can pass whatever you want to. This void* gets passed to the
        function when it is called. Since you passed a static function, you have no
        this pointer, and no way to get at the object. Pass the "this" pointer into
        that void*, cast it into the object (using dynamic_cast<> for safety) inside
        the callback function, and you can now get at you're object.

        Nifty no?

        Tony


        "Jeremy" <thevisualcore@ hotmail.com> wrote in message
        news:jH1Ka.1134 91$7n1.2320463@ twister.tampaba y.rr.com...[color=blue]
        > "Ron Natalie" <ron@sensor.com > wrote in message
        > news:Q4ydnVb9Vs QhCWWjXTWQkQ@gi ganews.com...[color=green]
        > >
        > > "David Hill" <david@wmol.com > wrote in message[/color]
        > news:2003062414 0611.71b47dfa.d avid@wmol.com.. .[color=green][color=darkred]
        > > > Hello -
        > > > I am using a library that takes a function pointer as an argument. Is[/color][/color]
        > the code below not possible?[color=green][color=darkred]
        > > >
        > > > void (*func)(int, short, void *) = a;
        > > >[/color]
        > >
        > > It is not possible. Member functions are different than regular[/color]
        > functions.[color=green]
        > > You can't convert between the two. What object is the member function
        > > going to be invoked on if you only have the member address?
        > >
        > > If you can't change the interface to the library, you're going to have[/color][/color]
        to[color=blue]
        > wrap the[color=green]
        > > member function call in an ordinary function. You can squirrel away[/color][/color]
        the[color=blue]
        > pointer[color=green]
        > > to member and "this" pointer in that extra void* operand I suspect.
        > >[/color]
        >
        > What does that mean (You can squirrel away the pointer to member and[/color]
        "this"[color=blue]
        > pointer in that extra void* operand I suspect)?
        > (i'm a newb, sorry)
        >
        >[/color]


        Comment

        • Pete Becker

          #5
          Re: function pointer

          Tony Di Croce wrote:[color=blue]
          >
          > Declare a member function of you're object as static. This will keep it from
          > getting a "this" pointer, and you'll be able to point a C style function
          > pointer at it.[/color]

          Well, maybe. But not portably. Member functions have C++ linkage, and a
          C library expects functions with C linkage. Most compilers don't
          distinguish between the two, so you can get away with doing this. But
          it's better to use a non-member function and mark it extern "C".

          --

          Pete Becker
          Dinkumware, Ltd. (http://www.dinkumware.com)

          Comment

          • Chandra Shekhar Kumar

            #6
            Re: function pointer

            >[color=blue]
            >
            > void Server::loop(vo id)
            > {
            > // void (*func)(int, short, void *) = a;[/color]

            it shud be:
            void (*func)(int, short, void *) = &Test::a;


            Comment

            • Rob Williscroft

              #7
              Re: function pointer

              Chandra Shekhar Kumar wrote in news:3EF89B7E.2 78E5C51@oracle. com:
              [color=blue][color=green]
              >>
              >>
              >> void Server::loop(vo id)
              >> {
              >> // void (*func)(int, short, void *) = a;[/color]
              >
              > it shud be:
              > void (*func)(int, short, void *) = &Test::a;
              >
              >[/color]

              Not true, there is no conversion from member-function-pointer
              to function-pointer, or from member-pointer to pointer for
              that matter.

              Rob.
              --

              Comment

              • Josephine Schafer

                #8
                Re: function pointer


                "Chandra Shekhar Kumar" <chandra.kumar@ oracle.com> wrote in message
                news:3EF89B7E.2 78E5C51@oracle. com...[color=blue][color=green]
                > >
                > >
                > > void Server::loop(vo id)
                > > {
                > > // void (*func)(int, short, void *) = a;[/color]
                >
                > it shud be:
                > void (*func)(int, short, void *) = &Test::a;
                >[/color]

                No, this should not work.
                My compiler issues this error message:
                "'initializ ing' : cannot convert from 'void (__thiscall
                Test::*)(int,sh ort,void *)' to 'void (__cdecl *)(int,short,vo id *)'
                There is no context in which this conversion is possible."


                Comment

                • Ron Natalie

                  #9
                  Re: function pointer


                  "Chandra Shekhar Kumar" <chandra.kumar@ oracle.com> wrote in message news:3EF89B7E.2 78E5C51@oracle. com...[color=blue][color=green]
                  > >
                  > >
                  > > void Server::loop(vo id)
                  > > {
                  > > // void (*func)(int, short, void *) = a;[/color]
                  >
                  > it shud be:
                  > void (*func)(int, short, void *) = &Test::a;
                  >[/color]
                  It still won't work. You can't assign a pointer to member to pointer to (non-member) function
                  no matter how you qualify it.


                  Comment

                  • Ron Natalie

                    #10
                    Re: function pointer


                    "Jeremy" <thevisualcore@ hotmail.com> wrote in message news:jH1Ka.1134 91$7n1.2320463@ twister.tampaba y.rr.com...[color=blue]
                    > "Ron Natalie" <ron@sensor.com > wrote in message
                    > news:Q4ydnVb9Vs QhCWWjXTWQkQ@gi ganews.com...[color=green]
                    > >
                    > > "David Hill" <david@wmol.com > wrote in message[/color]
                    > news:2003062414 0611.71b47dfa.d avid@wmol.com.. .[color=green][color=darkred]
                    > > > Hello -
                    > > > I am using a library that takes a function pointer as an argument. Is[/color][/color]
                    > the code below not possible?[color=green][color=darkred]
                    > > >
                    > > > void (*func)(int, short, void *) = a;
                    > > >[/color]
                    > >
                    > > It is not possible. Member functions are different than regular[/color]
                    > functions.[color=green]
                    > > You can't convert between the two. What object is the member function
                    > > going to be invoked on if you only have the member address?
                    > >
                    > > If you can't change the interface to the library, you're going to have to[/color]
                    > wrap the[color=green]
                    > > member function call in an ordinary function. You can squirrel away the[/color]
                    > pointer[color=green]
                    > > to member and "this" pointer in that extra void* operand I suspect.
                    > >[/color]
                    >
                    > What does that mean (You can squirrel away the pointer to member and "this"
                    > pointer in that extra void* operand I suspect)?
                    > (i'm a newb, sorry)
                    >[/color]

                    struct WrapperHelper {
                    Test* obj;
                    void (TEST::*fp)(int , short, void*);
                    void* arg;
                    } ;

                    void Wrapper(int i, short s, void* vp) {
                    WrapperHelper* sp = static_cast<Wra pperHelper*>(vp );
                    (sp->*func)(i, s, sp->arg);
                    delete sp;
                    }

                    void Test::SetupWrap per() {
                    WrapperHelper* sp = new WrapperHelper;
                    sp->obj = this;
                    sp->fp = &Test::func;
                    sp->arg = whatever.
                    library_func(&W rapper, sp);
                    }



                    Comment

                    Working...