Calling C++ functions in C function through function pointers

Discussion in 'C++' started by D3|\\||\\|!$, Feb 25, 2008.

  1. D3|\\||\\|!$

    D3|\\||\\|!$ Guest

    Hi All!!

    I have a C++ program that uses callback funtions which are the private
    members of class. The code uses an API wrtiiten in C which supplies
    callback-setting functions that require pointers to these functions...
    The funtions wherein these API's callback-setting functions are
    called, are public members of the same class of which the callbacks
    are the private member.

    Now the API functions are generating compile errors since they cannot
    "understand" the C++ function pointers.





    Consider the sample code:
    class classname
    {
    public :

    /* Constructor for the class */
    classname ();

    /* Destructor for the class */
    ~classname ();

    /* Function calling API's callback setting function */
    ReturnType1 Call_SetCallback (ReturnType2 arg1, ReturnType3
    arg2,...);

    private :

    /* Callback to be passed through function pointer */
    Callback (ReturnType4 arg1, ReturnType5 arg2,...);
    };

    ReturnType1 classname :: Call_SetCallback (ReturnType6 arg1,
    ReturnType7 arg2,...)
    {
    /* PtrToCallback is a function pointer to Callback
    (ReturnType4 arg1, ReturnType5 arg2,...)*/
    SetCallback (&classname::Callback, void *arg);
    }




    OUTPUT on compilation:
    error Error C2664: 'SetCallback' : cannot convert parameter 1 from
    'ReturnType (__thiscall classname::* )(ReturnType4,ReturnType5)' to
    'ReturnType1' FilePath\filename.cpp LineNumber




    The same code had earlier been running perfectly fine as C code - I
    simply moved the concerned functions to their specific places in the
    aforesaid class and now its running into trouble.

    Could somebody please suggest me a possible workaround..?? I cannot
    export the concerned private callbacks outside the function or make
    them public...

    Warm Regards,
    D3|\||\|!$
    D3|\\||\\|!$, Feb 25, 2008
    #1
    1. Advertising

  2. D3|\\||\\|!$

    Ondra Holub Guest

    On 25 Ún, 11:37, "D3|\\||\\|!$" <> wrote:
    > Hi All!!
    >
    > I have a C++ program that uses callback funtions which are the private
    > members of class. The code uses an API wrtiiten in C which supplies
    > callback-setting functions that require pointers to these functions...
    > The funtions wherein these API's callback-setting functions are
    > called, are public members of the same class of which the callbacks
    > are the private member.
    >
    > Now the API functions are generating compile errors since they cannot
    > "understand" the C++ function pointers.
    >
    > Consider the sample code:
    > class classname
    > {
    > public :
    >
    > /* Constructor for the class */
    > classname ();
    >
    > /* Destructor for the class */
    > ~classname ();
    >
    > /* Function calling API's callback setting function */
    > ReturnType1 Call_SetCallback (ReturnType2 arg1, ReturnType3
    > arg2,...);
    >
    > private :
    >
    > /* Callback to be passed through function pointer */
    > Callback (ReturnType4 arg1, ReturnType5 arg2,...);
    >
    > };
    >
    > ReturnType1 classname :: Call_SetCallback (ReturnType6 arg1,
    > ReturnType7 arg2,...)
    > {
    > /* PtrToCallback is a function pointer to Callback
    > (ReturnType4 arg1, ReturnType5 arg2,...)*/
    > SetCallback (&classname::Callback, void *arg);
    >
    > }
    >
    > OUTPUT on compilation:
    > error Error C2664: 'SetCallback' : cannot convert parameter 1 from
    > 'ReturnType (__thiscall classname::* )(ReturnType4,ReturnType5)' to
    > 'ReturnType1' FilePath\filename.cpp LineNumber
    >
    > The same code had earlier been running perfectly fine as C code - I
    > simply moved the concerned functions to their specific places in the
    > aforesaid class and now its running into trouble.
    >
    > Could somebody please suggest me a possible workaround..?? I cannot
    > export the concerned private callbacks outside the function or make
    > them public...
    >
    > Warm Regards,
    > D3|\||\|!$


    You are trying to use class member methods as ordinary functions. It
    is not possible. Pointer to class method is something different than
    pointer to function, because it may be called only together with class
    instance.

    The simple solution is to make callback functions static. Additionaly
    I would use as callback function declared as extern "C" (in your case).
    Ondra Holub, Feb 25, 2008
    #2
    1. Advertising

  3. D3|\\||\\|!$

    red floyd Guest

    D3|\||\|!$ wrote:
    > Hi All!!
    >
    > I have a C++ program that uses callback funtions which are the private
    > members of class. The code uses an API wrtiiten in C which supplies
    > callback-setting functions that require pointers to these functions...
    > The funtions wherein these API's callback-setting functions are
    > called, are public members of the same class of which the callbacks
    > are the private member.
    >
    > Now the API functions are generating compile errors since they cannot
    > "understand" the C++ function pointers.
    >
    >
    >
    > [redacted]


    This is a FAQ.
    http://www.parashift.com/c -faq-lite/pointers-to-members.html#faq-33.2
    red floyd, Feb 25, 2008
    #3
  4. D3|\\||\\|!$

    Ian Collins Guest

    Ondra Holub wrote:
    >
    > The simple solution is to make callback functions static. Additionaly
    > I would use as callback function declared as extern "C" (in your case).


    Static members have C++ linkage. C callbacks should have extern "C"
    linkage.

    Use a free function declared with extern "C" linkage. Make this a
    friend of the class if you want to call a private member function.

    --
    Ian Collins.
    Ian Collins, Feb 25, 2008
    #4
    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. Timothy Madden
    Replies:
    3
    Views:
    483
    Timothy Madden
    Dec 29, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,880
    Eric Sosman
    Jan 7, 2005
  3. Marc Thrun
    Replies:
    15
    Views:
    857
    Tim Rentsch
    Oct 4, 2005
  4. D3|\\||\\|!$
    Replies:
    20
    Views:
    930
    Gerry Ford
    Feb 29, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    669
Loading...

Share This Page