Callback functions problem

Discussion in 'C++' started by ajay.sonawane@gmail.com, Jun 6, 2005.

  1. Guest

    Hello ther
    I read somewhere that callback function should be global or static
    member function.
    1: I could use static member functions but how could I access members
    of class which ar not static.
    2: I could use global function but want to access members of class.
    Let me know the possible solutions.

    Regards,
    Ajay Sonawane
    , Jun 6, 2005
    #1
    1. Advertising

  2. Howard Guest

    <> wrote in message
    news:...
    > Hello ther
    > I read somewhere that callback function should be global or static
    > member function.
    > 1: I could use static member functions but how could I access members
    > of class which ar not static.
    > 2: I could use global function but want to access members of class.
    > Let me know the possible solutions.
    >
    > Regards,
    > Ajay Sonawane
    >

    Most callback functions that I've worked with provide a void* pointer, often
    called userData or something similar, which is there for you to use as you
    see fit. What I do is write a global callback function, and put in that
    void* the "this" pointer of the object which wants to receive the callbacks.
    Then, in the callback, I simply cast the void* pointer to a pointer to my
    class, and use it to call a member function on that object to do the actual
    work of the callback.

    -Howard
    Howard, Jun 6, 2005
    #2
    1. Advertising

  3. kloodge Guest

    You may try using a friend function. This would give you a global function
    that can also access member data.

    <> wrote in message
    news:...
    > Hello ther
    > I read somewhere that callback function should be global or static
    > member function.
    > 1: I could use static member functions but how could I access members
    > of class which ar not static.
    > 2: I could use global function but want to access members of class.
    > Let me know the possible solutions.
    >
    > Regards,
    > Ajay Sonawane
    >
    kloodge, Jun 6, 2005
    #3
  4. Howard Guest

    "kloodge" <> wrote in message
    news:mJZoe.27193$...

    Please don't top-post. [Re-arranged]

    > <> wrote in message
    > news:...
    >> Hello ther
    >> I read somewhere that callback function should be global or static
    >> member function.
    >> 1: I could use static member functions but how could I access members
    >> of class which ar not static.
    >> 2: I could use global function but want to access members of class.
    >> Let me know the possible solutions.
    >>
    >> Regards,
    >> Ajay Sonawane
    >>

    >


    > You may try using a friend function. This would give you a global function
    > that can also access member data.
    >


    While a friend function will allow you access to an object's private or
    protected members, it does not help in this case, because a static member or
    global function does not have an instance of that class to operate on. The
    question was about accessing non-static members, which means he needs an
    instance of the class to operate on. Friendship only affects visibility.

    -Howard
    Howard, Jun 6, 2005
    #4
  5. Rapscallion Guest

    Howard wrote:
    > Most callback functions that I've worked with provide a void* pointer, often
    > called userData or something similar, which is there for you to use as you
    > see fit. What I do is write a global callback function, and put in that
    > void* the "this" pointer of the object which wants to receive the callbacks.
    > Then, in the callback, I simply cast the void* pointer to a pointer to my
    > class, and use it to call a member function on that object to do the actual
    > work of the callback.


    This is a typical C solution. In C++ you have more and better (i.e.
    typesave) options.
    Rapscallion, Jun 6, 2005
    #5
  6. BigBrian Guest

    >This is a typical C solution. In C++ you have more and better (i.e.
    >typesave) options.


    I could be wrong, but I don't think you understand what Rapscallion
    posted. This is not a C solution. He's passing the address of a C++
    object into the registeration function of the library which is using a
    callback. That way when the callback is called, the libary passes back
    a pointer to this object which can then used in the callback.

    However, this solution depends on the interface of the registration
    code providing a user data parameter ( ie something which gets passed
    back when the callback is called ). This isn't always the case.

    -Brian
    BigBrian, Jun 6, 2005
    #6
  7. Howard Guest

    "Rapscallion" <> wrote in message
    news:...
    > Howard wrote:
    >> Most callback functions that I've worked with provide a void* pointer,
    >> often
    >> called userData or something similar, which is there for you to use as
    >> you
    >> see fit. What I do is write a global callback function, and put in that
    >> void* the "this" pointer of the object which wants to receive the
    >> callbacks.
    >> Then, in the callback, I simply cast the void* pointer to a pointer to my
    >> class, and use it to call a member function on that object to do the
    >> actual
    >> work of the callback.

    >
    > This is a typical C solution. In C++ you have more and better (i.e.
    > typesave) options.
    >


    The original question was about the callback function, whether it should be
    a static member function or a global function, and he was wondering about
    how to access non-static members from that function. I told him how I've
    generally implemented them and used them to accomplish the task.

    This method calls a C++ member function, so it's hardly typical C. And I
    can pass the void* pointer as a base class pointer if needed, so that
    run-time polymorphism can be used.

    It does require that there be a field useable to specify the object instance
    when "registering" for the callbacks. This field is usually specified as a
    void* pointer, to allow you to pass any kind of object at all for any
    purpose you desire. I pass the instance of my object. Since the entire
    callback is simply a one-liner that casts my pointer to my _known_ object
    type and calls its member function, I don't really have to worry about type
    safety. (Assuming I don't get someone else's callbacks...in which case
    either the callback mechanism is broken or I didn't RTFM.)

    Although you didn't specify an alternate method, I suppose you could use
    templates. I haven't thought it out, but it sounds like the best
    "all-purpose" solution to me, if you're sticking to C++ callback clients
    only.

    But I suspect that most API's I've worked with don't really care if you're
    calling their library from C or C++ (or even something else), so they just
    go with a generic void* pointer. And, I don't recall any API I've used
    implementing their callbacks as templates...so far they've all provided a
    void* pointer for my use.

    -Howard
    Howard, Jun 6, 2005
    #7
  8. Rapscallion Guest

    Howard wrote:
    > The original question was about the callback function, whether it should be
    > a static member function or a global function, and he was wondering about
    > how to access non-static members from that function. I told him how I've
    > generally implemented them and used them to accomplish the task.
    >
    > This method calls a C++ member function, so it's hardly typical C. And I
    > can pass the void* pointer as a base class pointer if needed, so that
    > run-time polymorphism can be used.


    You can easily avoid a void* pointer, in C and in C++. And, void* isn't
    a 'base class pointer', nor is it 'generic'.

    > Although you didn't specify an alternate method, I suppose you could use
    > templates. I haven't thought it out, but it sounds like the best
    > "all-purpose" solution to me, if you're sticking to C++ callback clients
    > only.


    Templates? I have no idea what you mean.
    Rapscallion, Jun 6, 2005
    #8
  9. Howard Guest

    "Rapscallion" <> wrote in message
    news:...
    > Howard wrote:
    >> The original question was about the callback function, whether it should
    >> be
    >> a static member function or a global function, and he was wondering about
    >> how to access non-static members from that function. I told him how I've
    >> generally implemented them and used them to accomplish the task.
    >>
    >> This method calls a C++ member function, so it's hardly typical C. And I
    >> can pass the void* pointer as a base class pointer if needed, so that
    >> run-time polymorphism can be used.

    >
    > You can easily avoid a void* pointer, in C and in C++. And, void* isn't
    > a 'base class pointer', nor is it 'generic'.
    >


    I never suggested that you couldn't avoid a void* pointer. But if the
    callback mechanism being used DOES use a void* pointer, then that's what it
    uses. (But recall that the OP was talking about the callback function
    itself, and never stated what the callback mechanism was, nor what the
    parameters passed to that function might be, so speculating is somewhat
    useless at this point.)

    And I never said void* was a base class pointer. Where did you get that
    idea? I said I could pass a base-class pointer AS a void* pointer, in the
    specific case I'm describing where a void* pointer is given to the user's
    callback function (and previously set by object during the registration
    process for the callback).

    Also, what isn't generic about a void* pointer? You can pass ANY pointer as
    a void* pointer. Sounds pretty generic to me.

    >> Although you didn't specify an alternate method, I suppose you could use
    >> templates. I haven't thought it out, but it sounds like the best
    >> "all-purpose" solution to me, if you're sticking to C++ callback clients
    >> only.

    >
    > Templates? I have no idea what you mean.
    >


    What do you not understand? Are you asking for clarification of something?
    Or, do you not know what templates are? Or, do you disagree that templates
    might provide a solution? Or,...?

    I guess I just fail to see if you're arguing something here, or asking for
    more information, or what. If you've got a specific point to make, make it.
    So far, you've made no specific recommendations. You're not providing any
    information that the OP can use, nor presenting any discussion that can be
    intelligently engaged in.

    -Howard
    Howard, Jun 7, 2005
    #9
  10. Guest

    Thanks Howard,
    Passing base class pointer as an extra parameter to callback function
    solved my problem. Its pretty easy. Sounds that it is "all time easiest
    solution".
    But one queston, Is "thunk" related to callback function ? If not
    what is it ? Someone just told me that thunk could be used to access
    members of class when you use callback functions.
    Awaiting for your comments.
    , Jun 7, 2005
    #10
  11. Howard Guest

    <> wrote in message
    news:...
    > Thanks Howard,
    > Passing base class pointer as an extra parameter to callback function
    > solved my problem. Its pretty easy. Sounds that it is "all time easiest
    > solution".
    > But one queston, Is "thunk" related to callback function ? If not
    > what is it ? Someone just told me that thunk could be used to access
    > members of class when you use callback functions.
    > Awaiting for your comments.
    >


    Glad I could help!

    The only place I recall hearing "thunk" was referring to running Win16
    applications in a Win32 environment, or something like that. Sorry, I don't
    know how it might be applied here. You could try searching on
    groups.google.com and see what others have said about it before.

    -Howard
    Howard, Jun 7, 2005
    #11
  12. In message <dPhpe.909176$>,
    Howard <> writes
    >
    ><> wrote in message
    >news:...
    >> Thanks Howard,
    >> Passing base class pointer as an extra parameter to callback function
    >> solved my problem. Its pretty easy. Sounds that it is "all time easiest
    >> solution".
    >> But one queston, Is "thunk" related to callback function ? If not
    >> what is it ? Someone just told me that thunk could be used to access
    >> members of class when you use callback functions.
    >> Awaiting for your comments.
    >>

    >
    >Glad I could help!
    >
    >The only place I recall hearing "thunk" was referring to running Win16
    >applications in a Win32 environment, or something like that. Sorry, I don't
    >know how it might be applied here. You could try searching on
    >groups.google.com and see what others have said about it before.


    IIRC (but it was a long time ago) the term is used in two ways:
    (a) compiler-generated code which transforms function arguments when
    passing them between incompatible interfaces (e.g. 16/32 bits, as above)

    (b) an anonymous compiler-generated function used by Algol 60(?) to
    allow an expression to be used as an actual argument passed by reference
    (and re-evaluated each time the formal parameter was used), giving the
    effect of a lambda function.

    --
    Richard Herring
    Richard Herring, Jun 7, 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. exquisitus
    Replies:
    23
    Views:
    47,555
    Andrew McDonagh
    Feb 28, 2005
  2. Pratik

    Callback functions

    Pratik, Jun 15, 2004, in forum: C++
    Replies:
    5
    Views:
    465
    E. Robert Tisdale
    Jun 15, 2004
  3. Xiangliang Meng
    Replies:
    1
    Views:
    1,595
    Victor Bazarov
    Jun 21, 2004
  4. cadull

    Member Callback Functions

    cadull, May 6, 2005, in forum: C++
    Replies:
    1
    Views:
    486
    cadull
    May 6, 2005
  5. Replies:
    1
    Views:
    372
Loading...

Share This Page