Thread: Ptr-to-member as callback in a c-function

  1. #1
    Registered User
    Join Date
    Nov 2006

    Ptr-to-member as callback in a c-function


    I'm using a c-library in my c++ project.
    Therefore I've to call this c-function:
    int rm_init(int id, char* ip, unsigned short port, int timeout,
    the signatures of the callback function is:
    void callback_fnc(int, int, int, char*)
    I want to use a class member as the callback-function and the call
    shall be done in the ctor of the same class:

    struct C
        void cb(int id, int cmd, int len, char* data);
    how would I call rm_init() from C() and give the member funtions
    (C::cb()) instead a c-funtion to it?
    I guess I need some bind or mem_fun magic, but Im not shure about ist.

    Thank you in advance!

  2. #2
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    the callback function should be declared as static, otherwise it has a hidden parameter this so its signature is different from the required...

    And because it is static - it has no pointer to the object - you should provide it if needed in some they, using the data pointer for example
    To be or not to be == true

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM