How can we define a funtion pointer to point a member function of a class?

Discussion in 'C++' started by Guest, Jul 14, 2004.

  1. Guest

    Guest Guest

    Say we have the following code defining TMyMsgHandler and TMyClass

    typedef void (*TOnMsgReceive) (TMyMessage Msg);
    class TMyMsgHandler
    {
    public:
    TMyMsgHandler();
    virtual ~TMyMsgHandler();
    TOnMsgReceive *OnMsgReceive;
    };

    class TMyClass1
    {
    public:
    TMyClass();
    ~TMyClass();
    void OnMsgReceive(TMyMessage Msg);
    };
    class TMyClass2
    {
    public:
    TMyClass2();
    ~TMyClass2();
    void OnMsgReceive(TMyMessage Msg);
    };

    ....
    TMyMsgHandler MyMsgHandler;
    TMyClass1 MyClass1;
    TMyClass2 MyClass2;

    MyMsgHandler.OnMsgReceive = MyClass.OnMsgReceive; <-- NOT VALID !!!

    How can we define a funtion pointer to point a member function of a class?
    Guest, Jul 14, 2004
    #1
    1. Advertising

  2. Karl Heinz Buchegger, Jul 14, 2004
    #2
    1. Advertising

  3. Guest

    Guest Guest

    Guest, Jul 14, 2004
    #3
  4. <Kay> wrote in message
    news:40f548ea$0$25461$...
    > Say we have the following code defining TMyMsgHandler and TMyClass
    >
    > typedef void (*TOnMsgReceive) (TMyMessage Msg);
    > class TMyMsgHandler
    > {
    > public:
    > TMyMsgHandler();
    > virtual ~TMyMsgHandler();
    > TOnMsgReceive *OnMsgReceive;
    > };
    >
    > class TMyClass1
    > {
    > public:
    > TMyClass();
    > ~TMyClass();
    > void OnMsgReceive(TMyMessage Msg);
    > };
    > class TMyClass2
    > {
    > public:
    > TMyClass2();
    > ~TMyClass2();
    > void OnMsgReceive(TMyMessage Msg);
    > };
    >
    > ...
    > TMyMsgHandler MyMsgHandler;
    > TMyClass1 MyClass1;
    > TMyClass2 MyClass2;
    >
    > MyMsgHandler.OnMsgReceive = MyClass.OnMsgReceive; <-- NOT VALID !!!
    >
    > How can we define a funtion pointer to point a member function of a class?
    >


    You cannot.

    You can do three things instead.

    Either you can change you code so that it uses a member function pointer
    instead of a function pointer, in your case that does not seem easy because
    your two classes TMyClass1 and TMyClass2 are unrelated.

    Or you can write a normal function that calls the member function that you
    want to be called.

    Or you can make the member functions static.

    In the first two cases there is the drawback that you must have some way of
    specifying which object you want to use to call the member function. Its
    this requirement that newbies usually manage to forget, but in C++ any
    non-static member function must be called on an object and its unreasonable
    to expect C++ to magic an object out of thin air for you. The third method
    gets round this problem by eliminating objects entirely. If that is your
    preferred solution then I would stick with simple function pointers instead.

    You could also read the FAQ

    http://www.parashift.com/c -faq-lite/pointers-to-members.html

    john
    John Harrison, Jul 14, 2004
    #4
  5. Guest

    Siemel Naran Guest

    <Kay> wrote in message
    news:40f548ea$0$25461$...

    > typedef void (*TOnMsgReceive) (TMyMessage Msg);
    > class TMyMsgHandler
    > {
    > public:
    > TMyMsgHandler();
    > virtual ~TMyMsgHandler();
    > TOnMsgReceive *OnMsgReceive;
    > };
    >
    > class TMyClass1
    > {
    > public:
    > TMyClass();
    > ~TMyClass();
    > void OnMsgReceive(TMyMessage Msg);
    > };
    > class TMyClass2
    > {
    > public:
    > TMyClass2();
    > ~TMyClass2();
    > void OnMsgReceive(TMyMessage Msg);
    > };
    >
    > ...
    > TMyMsgHandler MyMsgHandler;
    > TMyClass1 MyClass1;
    > TMyClass2 MyClass2;
    >
    > MyMsgHandler.OnMsgReceive = MyClass.OnMsgReceive; <-- NOT VALID !!!
    >
    > How can we define a funtion pointer to point a member function of a class?


    To learn about member pointers, read the FAQ as others pointed out. You can
    form a pointer that points to a member of a class, but to call the member
    you need an object.

    In the above scenario, you're asking for a pointer that points to a member
    of an object, and C++ does not support this natively. You can build your
    own though, and the basic idea is to write an abstract class with a pure
    virtual operator(), derive from it a template class that holds a
    pointer/reference to an object along with a pointer to a member function,
    and operator() calls the member function on the stored object.
    Alternatively, check out boost::function available at http://www.boost.org/.

    If you're using Borland and don't care about writing ANSI compatible code,
    then use their extension to support pointers to functions of objects. Then
    your code should work:

    > MyMsgHandler.OnMsgReceive = MyClass.OnMsgReceive; <-- NOT VALID !!!


    (Except MyClass is undefined, maybe you mean MyClass1?) So change

    > typedef void (*TOnMsgReceive) (TMyMessage Msg);


    to

    typedef __closure void (*TOnMsgReceive) (TMyMessage Msg);
    Siemel Naran, Jul 14, 2004
    #5
  6. Siemel Naran <> spoke thus:

    > If you're using Borland


    Heaven forbid, although the __closure extension does come in handy.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jul 14, 2004
    #6
  7. Guest

    Siemel Naran Guest

    "Christopher Benson-Manica" <> wrote in message

    > Siemel Naran <> spoke thus:
    >
    > > If you're using Borland

    >
    > Heaven forbid, although the __closure extension does come in handy.


    Yes, indeed. I used to think that it would be nice if this were part of the
    language, but one can always use boost::function instead. Most likely a
    native implementation of __closure would be faster than boost::function
    though.
    Siemel Naran, Jul 15, 2004
    #7
    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. Fraser Ross
    Replies:
    4
    Views:
    1,023
    Fraser Ross
    Aug 14, 2004
  2. Replies:
    12
    Views:
    710
    fluden
    Feb 11, 2005
  3. robin liu
    Replies:
    3
    Views:
    810
    Robin Liu
    Apr 21, 2006
  4. Lee Xuzhang
    Replies:
    5
    Views:
    322
    Kevin D. Quitt
    Jun 14, 2006
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,287
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page