473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Jul 19 '05 #1
4 3053
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

Jul 19 '05 #2

"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in message news:3F**************@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?
Jul 19 '05 #3
Ron Natalie wrote:
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in message news:3F**************@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

Jul 19 '05 #4
On Mon, 20 Oct 2003 17:34:22 GMT, Thomas Matthews
<Th**********************@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
No**********@Home.com
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
3
by: Jan-Henrik Grobe | last post by:
Hallo, I am coming to this newsgroup with a very strange Problem. I have two c++ files A.cpp and B.cpp....In A.cpp I am creating an openGL window and in B.cpp I have stored the callback...
11
by: Rajesh | last post by:
Dear All, Please let me know the advantage of function pointer? Is it fast calling function using function pointer? Is it possible to use function pointer to optimise code? Thanks and regards...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
12
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
4
by: default | last post by:
Hello All. I was looking around for a function like strtok() which would tokenize on the complete list of delimiters, rather than tokenize on *any* of the delimiters in the group. I ended up...
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.