Member Function Callback

Discussion in 'C++' started by Chris Morley, Sep 14, 2004.

  1. Chris Morley

    Chris Morley Guest

    Hi, I have always done my C++ class callbacks with the age old 'using this
    pointer in parameter of the class's static callback function' and
    typecasting it to get the specific instance.

    However I'm left wondering are there any better ways of doing this?

    Just before I left work today I came across functors, which look promising.
    I will have a proper look tomorrow, but I have a few questions in the mean
    time.

    Is it possible to use functor classes with standard, precompiled, c style
    callback code, passing them in as pointers? Or do I need to pass a pointer
    to the base class of the translating class and have to recompile code in
    order for the derefencing to work?

    I'm going to have a look online now for some examples whereby callbacks are
    made compile time safe, in the meantime (and indeed if I cant find any), can
    anyone recommend some good examples?

    Many thanks in advance,

    Chris
     
    Chris Morley, Sep 14, 2004
    #1
    1. Advertising

  2. Chris Morley wrote:
    > Hi, I have always done my C++ class callbacks with the age old 'using this
    > pointer in parameter of the class's static callback function' and
    > typecasting it to get the specific instance.
    >
    > However I'm left wondering are there any better ways of doing this?
    >
    > Just before I left work today I came across functors, which look promising.
    > I will have a proper look tomorrow, but I have a few questions in the mean
    > time.
    >
    > Is it possible to use functor classes with standard, precompiled, c style
    > callback code, passing them in as pointers?


    Usually no. Functors work "like functions" in template code. That's
    what they are good for.

    > Or do I need to pass a pointer
    > to the base class of the translating class and have to recompile code in
    > order for the derefencing to work?


    Not sure what you mean here, but probably yes.

    > I'm going to have a look online now for some examples whereby callbacks are
    > made compile time safe, in the meantime (and indeed if I cant find any), can
    > anyone recommend some good examples?


    "Callbacks" are the way to do things using standard library's "for_each"
    and similar algorithms. A good book on Standard Library should have many
    examples you're looking for.

    Victor
     
    Victor Bazarov, Sep 14, 2004
    #2
    1. Advertising

  3. "Chris Morley" <> wrote in message news:<sCH1d.159$>...
    > Hi, I have always done my C++ class callbacks with the age old 'using this
    > pointer in parameter of the class's static callback function' and
    > typecasting it to get the specific instance.
    >
    > However I'm left wondering are there any better ways of doing this?
    >
    > Just before I left work today I came across functors, which look promising.
    > I will have a proper look tomorrow, but I have a few questions in the mean
    > time.
    >
    > Is it possible to use functor classes with standard, precompiled, c style
    > callback code, passing them in as pointers?


    No.

    > Or do I need to pass a pointer
    > to the base class of the translating class and have to recompile code in
    > order for the derefencing to work?


    No.

    You can write a wrapper function that calls your functor. If your C style
    callback uses a void pointer argument, the wrapper function can cast it
    into the functor type and call that functor. Using some simple template
    tricks, all fo this can be done automatically for different functors.

    For an example how this can be done you could look at the FLTK library.
    This GUI toolkit uses C style callbacks with void pointers. An extension
    library "fl_connect" can be used to create wrapper functions and functors
    without any change to the existing library.

    > I'm going to have a look online now for some examples whereby callbacks are
    > made compile time safe, in the meantime (and indeed if I cant find any), can
    > anyone recommend some good examples?


    Check out Steve Dewhursts "Function pointers and function objects", available
    on his webside
    www.semantics.org
    in the Presentations section.

    > Many thanks in advance,
    >
    > Chris


    Uwe
     
    Uwe Schnitker, Sep 15, 2004
    #3
    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. prettysmurfed
    Replies:
    6
    Views:
    711
    prettysmurfed
    Jul 22, 2003
  2. Frank Neuhaus

    Callback to member function

    Frank Neuhaus, Aug 2, 2005, in forum: C++
    Replies:
    7
    Views:
    359
    Pete Becker
    Aug 3, 2005
  3. JDT
    Replies:
    6
    Views:
    562
    James Kanze
    Mar 29, 2007
  4. Ramesh
    Replies:
    11
    Views:
    4,299
    James Kanze
    Dec 27, 2008
  5. Angus
    Replies:
    3
    Views:
    713
    James Kanze
    Mar 2, 2009
Loading...

Share This Page