Calling C++ functions from C

P

Praveen Srinivasan

Hi all,
Are there any important issues that one should be aware of when calling C++
functions from C? In particular, I'm passing a function pointer to a C
routine in a library, and in that function I do C++ type stuff, e.g.
utilizing operator overloading. Is that reasonable, or are there some
pitfalls I need to watch out for?

Thanks,
Praveen Srinivasan
 
A

Attila Feher

Praveen said:
Hi all,
Are there any important issues that one should be aware of when
calling C++ functions from C? In particular, I'm passing a function
pointer to a C routine in a library, and in that function I do C++
type stuff, e.g. utilizing operator overloading. Is that reasonable,
or are there some pitfalls I need to watch out for?

Are you doing callbacks?

You will not be able to pass member function pointers (C has no idea of
them). But I guess you know that. Another thing to remember is that as
soon as you do not pass pointers but call by name from C, you will need to
make the function extern "C" to get rid of the name mangling.

A third issue (unfortunately not important in most of todays C++ code) is
that it is very unhealthy to let the C++ exceptions escape from your C++
function into C! So if you have ANY chance of exceptions either make the
function throw() (so C++ will stop the app) or catch them all and ignore
them (and return error to the caller???).

A
 
N

Noah Roberts

Attila said:
Are you doing callbacks?

You will not be able to pass member function pointers (C has no idea of
them).

I did not think it was possible to get member function pointers. How is
it done. It would certainly be easier than functors or multiple
inheritance.

NR
 
K

Kevin Goodsell

Attila said:
Are you doing callbacks?

You will not be able to pass member function pointers (C has no idea of
them). But I guess you know that. Another thing to remember is that as
soon as you do not pass pointers but call by name from C, you will need to
make the function extern "C" to get rid of the name mangling.

extern "C" does more than turn off name mangling, I think. I believe you
need it even when you are calling via a pointer to ensure the proper
calling conventions.

-Kevin
 
A

Attila Feher

Noah said:
I did not think it was possible to get member function pointers. How
is it done. It would certainly be easier than functors or multiple
inheritance.

It is possible, but it is rarely necessary. And as I have said: it is not
something compatible with C. Think of the member function pointer as a
"number" telling: if this mfptr is applied together with a proper object,
call function 4. That function 4 can be virtual etc. So it is not really a
pointer in the conventional sense of being an address to the first (machine
code) statement of a function.
 
D

David Cattarin

Praveen Srinivasan said:
Hi all,
Are there any important issues that one should be aware of when calling C++
functions from C? In particular, I'm passing a function pointer to a C
routine in a library, and in that function I do C++ type stuff, e.g.
utilizing operator overloading. Is that reasonable, or are there some
pitfalls I need to watch out for?

Thanks,
Praveen Srinivasan

You'll need to compile the file that contains main() with the C++ compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top