| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Franz" |
| Date: |
20 Oct 2003 06:55:23 AM |
| Object: |
Can I use a C++ function pointer instead of a C one |
Hello
I have some C code where I can register function pointers for
callback:
[c]
/* reaction.h */
...
typedef void (*FP) (int,int);
addReaction(FP);
...
Can I pass a "c++" function (not a member function) from c++ code to
'addRection()' ?
[c++]
/* x.hpp */
...
void xFunc(int a, int b);
...
/* x.cpp */
#include "x.hpp"
#include "reaction.h"
...
ini()
{
addReaction(&xFunc);
}
Or do I have to declare that function as extern "C":
[c++]
/* x.hpp */
...
extern "C" void xFunc(int a, int b);
...
Thanks
Franz
.
|
|
| User: "Thomas Matthews" |
|
| Title: Re: Can I use a C++ function pointer instead of a C one |
20 Oct 2003 07:22:56 AM |
|
|
Franz wrote:
Hello
I have some C code where I can register function pointers for
callback:
[c]
/* reaction.h */
...
typedef void (*FP) (int,int);
addReaction(FP);
...
Can I pass a "c++" function (not a member function) from c++ code to
'addRection()' ?
[snip]
Yes, you can pass a pointer to a non-member C++ function to a
C function. However, if the C function was compiled in a
separate translation unit with a C compiler you may have to
declare the function as 'extern "C";'.
Or do I have to declare that function as extern "C":
This would be the safest technique.
Thanks
Franz
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
.
|
|
|
| User: "Ron Natalie" |
|
| Title: Re: Can I use a C++ function pointer instead of a C one |
20 Oct 2003 09:22:02 AM |
|
|
"Thomas Matthews" <Thomas_MatthewsReallyHatesSpam@sbcglobal.net> wrote in message news:3F93D33B.7040000@sbcglobal.net...
Yes, you can pass a pointer to a non-member C++ function to a
C function. However, if the C function was compiled in a
separate translation unit with a C compiler you may have to
declare the function as 'extern "C";'.
You've lost me on that one. Just how do you get a C function compiled
into the same translation unit as a C++ function pointer?
.
|
|
|
| User: "Thomas Matthews" |
|
| Title: Re: Can I use a C++ function pointer instead of a C one |
20 Oct 2003 12:34:22 PM |
|
|
Ron Natalie wrote:
"Thomas Matthews" <Thomas_MatthewsReallyHatesSpam@sbcglobal.net> wrote in message news:3F93D33B.7040000@sbcglobal.net...
Yes, you can pass a pointer to a non-member C++ function to a
C function. However, if the C function was compiled in a
separate translation unit with a C compiler you may have to
declare the function as 'extern "C";'.
You've lost me on that one. Just how do you get a C function compiled
into the same translation unit as a C++ function pointer?
Sorry, let me clarify my thoughts.
My understanding in this scenario is that there is a module,
lets say, old_func.c, which contains a non-member function:
void My_Function(void)
{
/* yada, yada, yada */
}
Let a C compiler (or a C++ compiler compiling in C mode)
translate this into old_func.o.
The function has the C language naming convention because
it was compiled using a C compiler.
If it was compiled using a C++ compiler, it _may_ have
a different {mangled} name than the C version.
Sooo, my point was that a function pointer in C++
may have an issue between when pointing to a function
compiled in C languge mode versus when it was
compiled in C++ mode. At the execution level, there
should be no difference. I believe only the Linker
{should there be one} would be the part that "chokes"
because of the naming conventions.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
.
|
|
|
| User: "Bob Hairgrove" |
|
| Title: Re: Can I use a C++ function pointer instead of a C one |
20 Oct 2003 03:31:55 PM |
|
|
On Mon, 20 Oct 2003 17:34:22 GMT, Thomas Matthews
<Thomas_MatthewsHatesSpam@sbcglobal.net> wrote:
My understanding in this scenario is that there is a module,
lets say, old_func.c, which contains a non-member function:
void My_Function(void)
{
/* yada, yada, yada */
}
Let a C compiler (or a C++ compiler compiling in C mode)
translate this into old_func.o.
The function has the C language naming convention because
it was compiled using a C compiler.
If it was compiled using a C++ compiler, it _may_ have
a different {mangled} name than the C version.
Sooo, my point was that a function pointer in C++
may have an issue between when pointing to a function
compiled in C languge mode versus when it was
compiled in C++ mode. At the execution level, there
should be no difference. I believe only the Linker
{should there be one} would be the part that "chokes"
because of the naming conventions.
If the C function is not exported from the C++ module, it should link
OK because most compilers will compile and link the function with C
linkage. This usually happens automatically when a file has a ".c" and
not a ".cpp" extension.
If the C function is being imported from an older library, it must be
imported with C linkage (extern "C"...). It should have a
corresponding prototype in your C++ code. You may need to inclose the
#include pre-processor directive in something like this:
#ifdef __cplusplus
{ extern "C"
#endif
#include "OldHeader.h"
#ifdef __cplusplus
}
#endif
--
Bob Hairgrove
NoSpamPlease@Home.com
.
|
|
|
|
|
|
|

|
Related Articles |
|
|