|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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|\||\|!$ |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 25 Ún, 11:37, "D3|\\||\\|!$" <e.kaba...@gmail.com> 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). |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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-lit....html#faq-33.2 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|