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

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

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

    Hi,

    I'm using a c-library in my c++ project.
    Therefore I've to call this c-function:
    Code:
    int rm_init(int id, char* ip, unsigned short port, int timeout,
    callaback_fnc);
    the signatures of the callback function is:
    Code:
    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:

    Code:
    struct C
    {
        void cb(int id, int cmd, int len, char* data);
    
         C()
         {
              ....
        }
    };
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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 Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM