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

This is a discussion on Ptr-to-member as callback in a c-function within the C++ Programming forums, part of the General Programming Boards category; 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* ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21