how does a call from c++ to c work internally??
+3
A:
The C++ compiler 'does the right thing' and uses the correct calling convention for the C function - a lame sounding answer but I don't know that there's much more that can be said!
Will A
2010-08-16 22:53:09
+1
A:
It is a heavy duty implementation detail. But most C++ compilers I know don't try to do anything special to differentiate a C function from a non-instance C++ function. Just the plain olden cdecl calling convention for both.
Kinda important because the CRT implementation, with functions like printf(), are just as usable by a C compiler as they are by the C++ compiler from the same vendor. Nobody wants to maintain two versions of it.
Hans Passant
2010-08-16 23:03:57
For reference, how does `extern "C"` factor in?
cHao
2010-08-16 23:06:04
@cHao it stops the name mangling that c++ does to support operator overloading, e.g. `foo(int)` and `foo(double)` might translate to `foo_int` and `foo_double` in the assembly emitted by the compiler, but `extern "C" foo(int)` and `extern "C" foo(double)` both translate simply to `foo`
Scott Wales
2010-08-16 23:13:27
It changes the name of the identifier as seen by the linker. Turns off the C++ name mangling. All traditional CRT functions are extern "C" in their declaration as seen by the C++ compiler.
Hans Passant
2010-08-16 23:14:22