Callback into C++ from C

A

Anonymous

I have legacy C code that performs some required functionality. I want
to notify my C++ classes after work has been completed in the C code. In
otherwords, I need to call a C++ method from C.

Some PSEUDO code may help:


/* C++ code */
class MyFooClass
{
public:
NotifyHandler();
};



/* C code */

#ifdef __cplusplus
extern "C" {
#endif

void foo()
{
/* Do some work */
/* finish work */
/* Notify C++ class via callback here */
}

#ifdef __cplusplus
};
#endif
 
V

Victor Bazarov

Anonymous said:
I have legacy C code that performs some required functionality. I want
to notify my C++ classes after work has been completed in the C code.
In otherwords, I need to call a C++ method from C.

Some PSEUDO code may help:


/* C++ code */
class MyFooClass
{
public:
NotifyHandler();
};



/* C code */

#ifdef __cplusplus
extern "C" {
#endif

void foo()
{
/* Do some work */
/* finish work */
/* Notify C++ class via callback here */
}

#ifdef __cplusplus
};
#endif

First off, there is some information on callbacks in the FAQ,
have you read it? If not, please do.

Second, the simplest way is to compile your so called C code
as C++ and be done with it, then you can pass your object in
and call anything you want from it.

If that's not something you want or are prepared to do, you
could play the static_cast game, where your class has a static
member function with a 'void*' argument. Inside it casts the
pointer to an object of its type ('MyFooClass') and calls the
member function. The C will need a void* passed to it so it
can pass it "back" to the static function.

V
 
C

Chris Thomasson

Victor Bazarov said:
Anonymous wrote: [...]
If that's not something you want or are prepared to do, you
could play the static_cast game, where your class has a static
member function with a 'void*' argument.

[...]

How can a static member function be extern "C"?
 
I

Ian Collins

Chris said:
Victor Bazarov said:
Anonymous wrote: [...]
If that's not something you want or are prepared to do, you
could play the static_cast game, where your class has a static
member function with a 'void*' argument.

[...]

How can a static member function be extern "C"?
It can't, a better alternative is to use a friend function.
 
P

Pete Becker

Chris said:
Victor Bazarov said:
Anonymous wrote: [...]
If that's not something you want or are prepared to do, you
could play the static_cast game, where your class has a static
member function with a 'void*' argument.

[...]

How can a static member function be extern "C"?
It can't, a better alternative is to use a friend function.

Use a free function. Whether that function also has to be a friend is
an implementation detail.
 

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,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top