Extern C method and struct

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?

+5
source share
4 answers

No, because it getNumis a member function that C does not have.

++ foo foo* ( foo ) C ( , , ++, C), ++ foo_getNum -, foo* ( C ), getNum. , , , ( foo*, foo , , void* - ).

+7

extern "C" -: getNum() ++.

++ (++ 03 §7.5/4):

C - .

, , C (, , C, C -). , , C .

+6

- C . ++-, .

+3

, , 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
+1
source

All Articles