Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Calling C++ functions in C function through function pointers (http://www.velocityreviews.com/forums/t594009-calling-c-functions-in-c-function-through-function-pointers.html)

D3|\\||\\|!$ 02-25-2008 10:38 AM

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

MisterE 02-25-2008 11:25 AM

Re: Calling C++ functions in C function through function pointers
 
somebody please suggest me a possible workaround..?? I cannot
> export the concerned private callbacks outside the function or make
> them public...


This is offtopic here, try comp.lang.c.and.cpp



Wolfgang Draxinger 02-25-2008 01:47 PM

Re: Calling C++ functions in C function through function pointers
 
D3|\||\|!$ wrote:

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


Even though this is the C newsgroup, I'm now giving you a heads
up on C++: You CAN'T use class member functions for a C-style
callback.

Think about it: How would the callback function know, on which
instance of the class to operate? The proper way to do it is to
write a friend wrapper callback function, that takes a pointer
to the class instance as parameter and calls the member function
on that instance. If you want to keep it generic, let it take
two parameters: The instance and a pointer-to-member (this is
different from a ordinary C pointer, as this is more an offset,
than a absolute pointer) in form of void*, typecast those to the
proper types and call it.

class foo
{
int bar();
friend int foo_bar_wrapper(void * i_);
};

int foo_bar_wrapper(void * i_)
{
foo * instance = dynamic_cast<foo*> i_;
return instance->bar();
}

void register_callback(int(*p)(), void * data);

I'm too lazy, to give an example of the pointer-to-member method
here. Personally I actually discourage it's use, as it's prone
to introduce a lot of nasty to track down bugs.

Wolfgang Draxinger
--
E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867


Default User 02-25-2008 05:49 PM

Re: Calling C++ functions in C function through function pointers
 
MisterE wrote:

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

>
> This is offtopic here, try comp.lang.c.and.cpp


There is no such newsgroup.




Brian

Keith Thompson 02-25-2008 05:50 PM

Re: Calling C++ functions in C function through function pointers
 
"D3|\||\|!$" <e.kabarie@gmail.com> writes:
> 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.

[...]

As it happens, C++ provides mechanisms for calls between C and C++
programs; C does not. So your question is more suitable for
comp.lang.c++.

Ordinary C-to-C++ and C++-to-C function calls are covered in the C++
FAQ. Member functions are going to be trickier; consider a wrapper.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

fred.l.kleinschmidt@boeing.com 02-25-2008 06:08 PM

Re: Calling C++ functions in C function through function pointers
 
On Feb 25, 2:38*am, "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|\||\|!$


This is a very common problem when using, for example, GUI toolkits
written in C. The easiest solution is to creeate a static function
in your class, where one of the parameters for that function is
the 'this' pointer.

An example using X/Motif:

In your C++ class header:
class MyClass {
public:
static void MyStaticFunction( Widget, XtPointer, XtPointer);
protected:
void MyRealFunction(...);
+

In your C++ code:
// Xt ships the 4th paramater as the clientData to
// callback functions
XtAddCallback( widget, XmNsomeCallback, MyStaticFunction, this );

Then, elsewhere in your C++ code, define the static function:

void MyStaticFunction( Widget w, XtPointer clientData, XtPointer
callData)
(
MyClass *me = (MyClass *)callData;
me->MyRealFunction(...)
)

--
Fred Kleinschmidt

Ian Collins 02-25-2008 06:14 PM

Re: Calling C++ functions in C function through function pointers
 
fred.l.kleinschmidt@boeing.com wrote:
>
> This is a very common problem when using, for example, GUI toolkits
> written in C. The easiest solution is to creeate a static function
> in your class, where one of the parameters for that function is
> the 'this' pointer.
>
> An example using X/Motif:
>
> In your C++ class header:
> class MyClass {
> public:
> static void MyStaticFunction( Widget, XtPointer, XtPointer);


While OT, this isn't strictly correct. The only type of function you
should pass to C from C++ is a function declared with extern "C"
linkage. Static member functions have C++ linkage, so use a friend instead.

A decent C++ compiler will issues a diagnostic if you pass anything else.

--
Ian Collins.

Wolfgang Draxinger 02-25-2008 06:29 PM

Re: Calling C++ functions in C function through function pointers
 
Ian Collins wrote:

> fred.l.kleinschmidt@boeing.com wrote:
>>
>> This is a very common problem when using, for example, GUI
>> toolkits written in C. The easiest solution is to creeate a
>> static function in your class, where one of the parameters for
>> that function is the 'this' pointer.
>>
>> An example using X/Motif:
>>
>> In your C++ class header:
>> class MyClass {
>> public:
>> static void MyStaticFunction( Widget, XtPointer,
>> XtPointer);

>
> While OT, this isn't strictly correct. The only type of
> function you should pass to C from C++ is a function declared
> with extern "C"
> linkage. Static member functions have C++ linkage, so use a
> friend instead.


Could it be, that you mistake linkage for registering a callback,
which are completely different things?

extern "C" means nothing else, than to disable name mangling,
which is the only difference between C and C++ linkage. If you
know the mangled name of a C++ function you could as well
declare that for use with C. The whole idea behind extern "C" is
to disable it. It also means, that you can't overload functions
in a extern "C" block, as overloading is implemented through
name mangling.

As long no class instances are concerned the calling convention
for C++ functions is the same as for C. A static member function
is technically an ordinary function, with the additional twist,
that it lives in the class' namespace and thus has access to
protected and private members of any instance of that class.
Remember that C++ restricts access to class members at compile
time.

Wolfgang Draxinger
--
E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867


Ian Collins 02-25-2008 06:42 PM

Re: Calling C++ functions in C function through function pointers
 
Wolfgang Draxinger wrote:
> Ian Collins wrote:
>
>> fred.l.kleinschmidt@boeing.com wrote:
>>> This is a very common problem when using, for example, GUI
>>> toolkits written in C. The easiest solution is to creeate a
>>> static function in your class, where one of the parameters for
>>> that function is the 'this' pointer.
>>>
>>> An example using X/Motif:
>>>
>>> In your C++ class header:
>>> class MyClass {
>>> public:
>>> static void MyStaticFunction( Widget, XtPointer,
>>> XtPointer);

>> While OT, this isn't strictly correct. The only type of
>> function you should pass to C from C++ is a function declared
>> with extern "C"
>> linkage. Static member functions have C++ linkage, so use a
>> friend instead.

>
> Could it be, that you mistake linkage for registering a callback,
> which are completely different things?
>

No, I've been putting people straight on this for years over on c.l.c++.

> extern "C" means nothing else, than to disable name mangling,
> which is the only difference between C and C++ linkage.


There is nothing in either standard to say that. A C++ implementation
is free to use a different calling convention from C if it chooses to do
so. That's why the linkage specifier is there in the language.
>
> As long no class instances are concerned the calling convention
> for C++ functions is the same as for C.


As I said, it might be and typically is, but there is no guarantee.

--
Ian Collins.

Gerry Ford 02-25-2008 08:49 PM

Re: Calling C++ functions in C function through function pointers
 

"Ian Collins" <ian-news@hotmail.com> wrote in message
news:62ggh6F22rk75U1@mid.individual.net...
> Wolfgang Draxinger wrote:


>> Could it be, that you mistake linkage for registering a callback,
>> which are completely different things?
>>

> No, I've been putting people straight on this for years over on c.l.c++.
>
>> extern "C" means nothing else, than to disable name mangling,
>> which is the only difference between C and C++ linkage.

>
> There is nothing in either standard to say that. A C++ implementation
> is free to use a different calling convention from C if it chooses to do
> so. That's why the linkage specifier is there in the language.
>>
>> As long no class instances are concerned the calling convention
>> for C++ functions is the same as for C.

>
> As I said, it might be and typically is, but there is no guarantee.
>
> --
> Ian Collins.


I think this question was multiposted to clc++, and through some strange
planetary alignment, I'm reading both forums now. There is no clc.and.cpp,
so don't waste your time like Brian and I did.

I think if OP does what Ian suggests, he'll get farther along in his
project.

--
Gerry Ford

"Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
beziehend.




All times are GMT. The time now is 12:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.