PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Calling C++ functions in C function through function pointers
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Calling C++ functions in C function through function pointers

Réponse
 
LinkBack Outils de la discussion
Vieux 25/02/2008, 10h37   #1
D3|\\||\\|!$
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Calling C++ functions in C function through function pointers

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|\||\|!$
  Réponse avec citation
Vieux 25/02/2008, 12h22   #2
Ondra Holub
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling C++ functions in C function through function pointers

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).
  Réponse avec citation
Vieux 25/02/2008, 16h22   #3
red floyd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling C++ functions in C function through function pointers

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
  Réponse avec citation
Vieux 25/02/2008, 18h19   #4
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling C++ functions in C function through function pointers

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h20.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,42443 seconds with 12 queries