By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Calling C++ functions from C

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Praveen Srinivasan wrote:
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
Jul 19 '05 #2

P: n/a
Attila Feher wrote:
Praveen Srinivasan wrote:
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).


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

Jul 19 '05 #3

P: n/a
Attila Feher wrote:
Praveen Srinivasan wrote:
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.


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4

P: n/a
Noah Roberts wrote:
Attila Feher wrote:
Praveen Srinivasan wrote:
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).


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.

--
Attila aka WW
Jul 19 '05 #5

P: n/a
Praveen Srinivasan <pr******@stanford.edu> wrote in message news:<bh**********@news.Stanford.EDU>...
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.
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.