Given the following C++ code,
#ifdef __cplusplus
extern "C" {
#endif
struct foo {
void getNum() {
}
};
#ifdef __cplusplus
}
#endif
int main (int argc, char * const argv[]) {
return 0 ;
}
Is it possible to call getNum() from C?
No, since
getNumis a member function, which C doesn’t have.A possible solution to that problem is to write a C++ function to return a
fooinstance as afoo*(wherefoois changed to be an empty struct) to C (I assume this is binary compiled as C++ to which C is linking), then have a free function in C++ calledfoo_getNumor something, which takes afoo*(whose definition is modified for the C version to be empty) which callsgetNumon it. It wouldn’t be type safe though, obviously, (but taking afoo*even whenfoois empty would be better thanvoid*– thanks David).