Howto declare a function pointer to a method in a class?

Discussion in 'C++' started by Francois Grieu, Oct 25, 2011.

  1. Hello,

    I'm trying to define a type suitable for a pointer to one
    of several member functions of my class cBar (all functions
    have the same interface, say accept an int and return void).


    For now, I'm making a type tHandler suitable for a pointer
    to a global function accepting an additional parameter "me",
    holding a pointer to my class:


    typedef void(*tHandler)(class cBar *const me, int val);

    void Handler0(class cBar *const me, int val);
    void Handler1(class cBar *const me, int val);

    class cBar {
    tHandler fCurrentHandler;
    /*..*/
    public:
    inline void cBar::CurrentHandler(int val)
    {
    (*fCurrentHandler)(this,val);
    }
    inline cBar()
    {
    fCurrentHandler = Handler0;
    CurrentHandler(0);
    }
    inline ~cBar()
    {
    CurrentHandler(-1);
    }
    };


    This works, but is ugly; in particular Handler0/Handler1 should
    be private methods of cBar, and tHandler a private type.

    Any idea ?

    TIA,
    Francois Grieu


    Note: this is not homework, I'm moving to C++ from an embedded C
    background.
    Francois Grieu, Oct 25, 2011
    #1
    1. Advertising

  2. On 10/25/2011 9:36 AM, Francois Grieu wrote:
    > I'm trying to define a type suitable for a pointer to one
    > of several member functions of my class cBar (all functions
    > have the same interface, say accept an int and return void).


    Does this not work

    typedef void (cBar::*tHandler)(int);

    for some reason? What book on C++ are you reading that doesn't explain
    pointers to member functions?

    >[..]


    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 25, 2011
    #2
    1. Advertising

  3. On 25/10/2011 15:41, Victor Bazarov wrote:
    > On 10/25/2011 9:36 AM, Francois Grieu wrote:
    >> I'm trying to define a type suitable for a pointer to one
    >> of several member functions of my class cBar (all functions
    >> have the same interface, say accept an int and return void).

    >
    > Does this not work
    >
    > typedef void (cBar::*tHandler)(int);
    >
    > for some reason?


    Yes that's it!

    > What book on C++ are you reading that doesn't explain
    > pointers to member functions?


    C++ For Mathematicians - An Introduction.. by Edward Scheinerman.

    Many thanks.

    Francois Grieu
    Francois Grieu, Oct 25, 2011
    #3
  4. Francois Grieu

    Christopher Guest

    On Oct 25, 8:54 am, Francois Grieu <> wrote:
    > On 25/10/2011 15:41, Victor Bazarov wrote:
    >
    > > On 10/25/2011 9:36 AM, Francois Grieu wrote:
    > >> I'm trying to define a type suitable for a pointer to one
    > >> of several member functions of my class cBar (all functions
    > >> have the same interface, say accept an int and return void).

    >
    > > Does this not work

    >
    > >      typedef void (cBar::*tHandler)(int);

    >


    You can also check out boost::function and boost::bind
    Christopher, Oct 25, 2011
    #4
  5. On 10/25/2011 9:54 AM, Francois Grieu wrote:
    > On 25/10/2011 15:41, Victor Bazarov wrote:
    >> On 10/25/2011 9:36 AM, Francois Grieu wrote:
    >>> I'm trying to define a type suitable for a pointer to one
    >>> of several member functions of my class cBar (all functions
    >>> have the same interface, say accept an int and return void).

    >>
    >> Does this not work
    >>
    >> typedef void (cBar::*tHandler)(int);
    >>
    >> for some reason?

    >
    > Yes that's it!
    >
    >> What book on C++ are you reading that doesn't explain
    >> pointers to member functions?

    >
    > C++ For Mathematicians - An Introduction.. by Edward Scheinerman.


    Get something that has more C++ in it. There are many decent books that
    go into enough depth on C++ to be recommended by www.ACCU.org (see their
    book section).

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 25, 2011
    #5
  6. Francois Grieu

    Krice Guest

    On 25 loka, 16:36, Francois Grieu <> wrote:
    > This works, but is ugly;


    It's ugly, because it's not C++. Learn more about C++, should do
    the trick.

    > Note: this is not homework, I'm moving to C++ from an embedded C
    > background.


    I can see that.
    Krice, Oct 25, 2011
    #6
    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. John Black
    Replies:
    2
    Views:
    356
    jeffc
    Jun 15, 2004
  2. Sam Waller
    Replies:
    2
    Views:
    432
    Stuart Golodetz
    Jul 14, 2006
  3. Pawel_Iks
    Replies:
    3
    Views:
    521
    terminator
    Jul 31, 2007
  4. Replies:
    4
    Views:
    419
    Victor Bazarov
    Sep 30, 2007
  5. Replies:
    2
    Views:
    652
    Triple-DES
    Feb 26, 2008
Loading...

Share This Page