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 5 4612
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
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
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: J.P. |
last post by:
Hi,
I found a function with the following header:
function checkLogin($username = '',$password = '',$groupid =
10,$goodRedirect = '',$badRedirect = '')
Now the writer of this function is...
|
by: Charles Larry |
last post by:
Is there a way to define functions after the main part of a Python
script?
Example:
#!/usr/local/bin/python
# this code yields a NameError
print_message("hello world")
|
by: bart_nessux |
last post by:
I need a script to call several functions at the same time. How does one
call more than one function simultaneously?
|
by: Ben |
last post by:
I am having trouble calling functions in another file.. Lets look at
the scenario below:
File xyz.cc: (I have both xyz.h and xyz.cc, for simplicity I've
avoided xyz.h)
------------
struct xyz...
|
by: Andrew Slade |
last post by:
Hello,
I am making calls from one compilation unit to functions in another by
pointers and I get the warnings below from gcc on 2.4.x Debian Linux. The
executable seems to work fine but the...
|
by: Dave |
last post by:
does calling a regular function cost any cpu time? In other words, is it
faster to write the code of two functions into main(), or is it the exact
same thing as calling two functions. I know its...
|
by: Kenneth Brody |
last post by:
The recent thread on "query about main()" got me thinking...
As I recall, calling a function with the wrong parameters causes
undefined behavior.
(These all assume that no prototype of foo()...
|
by: =?Utf-8?B?SnVsaWFu?= |
last post by:
My first problem is: I am having an issue when using a web service. The web
service is written by another department and say its running fine but when I
call it, I sometimes I get a...
|
by: bgold |
last post by:
Hey. I have a base class (SPRITE), and using this base class I have
derived a large number of derived classes (PERSON, BULLET, MISSILE,
etc.). Now, at a certain point in my program, I have a pair...
|
by: mreeves000 |
last post by:
Greetings,
Ok so, what I am trying to do is have one file that stores several functions so I can use them across the entire site.
So first I have the page, in every page I call header.php, and...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |