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?
getNum()
No, because it getNumis a member function that C does not have.
getNum
++ foo foo* ( foo ) C ( , , ++, C), ++ foo_getNum -, foo* ( C ), getNum. , , , ( foo*, foo , , void* - ).
foo
foo*
foo_getNum
void*
extern "C" -: getNum() ++.
extern "C"
++ (++ 03 §7.5/4):
C - .
, , C (, , C, C -). , , C .
- C . ++-, .
, , C, C struct. ++, C-. 2 main.c abc.cpp
main.c
extern "C" void getNumCaller(); int main () { getNumCaller(); return 0; }
abc.cpp
#include <iostream> struct foo { void getNum() { std::cout << "calling getNum" << std::endl; } }; extern "C" void getNumCaller() { struct foo abc; abc.getNum(); }
:
g++ -o abc abc.cpp main.c
and you will get the result:
calling getNum