Can I use a C++ function pointer instead of a C one

F

Franz

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
 
T

Thomas Matthews

Franz said:
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
 
R

Ron Natalie

Thomas Matthews said:
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?
 
T

Thomas Matthews

Ron said:
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
 
B

Bob Hairgrove

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
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top