0

I have a c++ class and c code as well. Following is rough (logically the same) and minimalised code

// C++ class - Car.cpp
void Car :: initialise() 
{ 
  WheelT mWheel;   // WheelT is a struct in Wheel.c
  mWheel.run(wheelGotFlat);    // <---- I want to pass here the c++ callback method , so that if the             wheel goes flat , the callback should hit 
}
static void car :: wheelGotFlat()   // <--- callback method
{
}

// C code - Wheel.c

void checkStatus(callback aCb)
{
   // if wheel is flat 
   // ----- here I want to hit the callback method that was passed as argument to run()
}
void run(callback aCb)
{                      
    checkStatus(aCb);       
}

How to do this ??

Subbu
  • 2,063
  • 4
  • 29
  • 42

1 Answers1

0

To make a C++ function callable from C, use extern "C" void foo(int) { ... }, for example. To make it call a member function, you'll have to make the callback a stub written in C++, but callable from C, that then calls the member function.

Note that using a static member function will often work, but is actually undefined behavior. See this for more details.

Community
  • 1
  • 1
kec
  • 2,099
  • 11
  • 17