Calling C++ functions in C function through function pointers

D

D3|\\||\\|!$

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|\||\|!$
 
O

Ondra Holub

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).
 
R

red floyd

D3|\||\|!$ said:
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
 
I

Ian Collins

Ondra said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top