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(&xF unc);
}
Or do I have to declare that function as extern "C":
[c++]
/* x.hpp */
...
extern "C" void xFunc(int a, int b);
...
Thanks
Franz 4 3069
"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote in message news:3F******** ******@sbcgloba l.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?
Ron Natalie wrote: "Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote in message news:3F******** ******@sbcgloba l.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(voi d)
{
/* 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.l earn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book
On Mon, 20 Oct 2003 17:34:22 GMT, Thomas Matthews
<Th************ **********@sbcg lobal.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(voi d) { /* 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**********@Ho me.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 member function?
|
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 functions. Important is, that the object
A.o has to be created BEFORE B can be compiled. I am sure that sounds
confusing but A.cpp is part of a library that is needed for objects I use in
B (kind of client-server thing). So I cannot include a B.h in the...
|
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
Rajesh
|
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 called, can be a genuine no-op.
Consider:
typedef int(*polymorphic_func)(int param);
|
by: mohan |
last post by:
Hi All,
How to implement virtual concept in c.
TIA
Mohan
| |
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' that takes arguments of type
(void *) because the ADT must be able to deal with any type of data.
In my actual code, I will code the function to take arguments of their
real types, then when I pass this pointer through an interface
function, I...
|
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 just rolling a function. Thought I would
post it here
for discussion.
Thanks.
|
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>
#include<stdlib.h>
#include<math.h>
|
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 about the syntax
of calling the same.
#include <stdio.h>
void fp1()
|
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 pointer array
instead of switch.
They believe that ordinary function pointer is much faster than
switch. Think of one variable called selectFunction. selectFunction
variable can be the range of 0 through 1,000 or more.
Load selectFunction...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |