473,383 Members | 1,789 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,383 software developers and data experts.

Passing a C++ object's member function to a C function expecing a function pointer!

Sounds nasty doesn't it!! Well it's kinda what I need to do! I have an
external C struct (external to the C++ project/classes etc.) which is
wants a function ptr assigned to one of it's members:

struct blah {
int (func_ptr*) (int a, int b, float c);
}; etc...

However, my code is in C++ and a wish to assign a non-static member
function (I know I can do this) to this struct member! Can this be
done!? Some how with pointers or references? What about the STL's
mem_func stuff?

Can anyone shed some light on this?

Cheers

Jim
Jul 22 '05 #1
7 1534
James,

"James Vanns" <ji**@canterbury.ac.uk> wrote in message
news:2d**************************@posting.google.c om...
Sounds nasty doesn't it!! Well it's kinda what I need to do! I have an
external C struct (external to the C++ project/classes etc.) which is
wants a function ptr assigned to one of it's members:

struct blah {
int (func_ptr*) (int a, int b, float c);
}; etc...

However, my code is in C++ and a wish to assign a non-static member
function (I know I can do this)
And how could that be?

The closest "non-static member function" would have the signature:

int (*fnc_ptr)( ThisClass*, int, int, float );

Where would the this pointer come from?
to this struct member! Can this be
done!? Some how with pointers or references? What about the STL's
mem_func stuff?
"mem_fun" results in a function object with an operator(), not even close to
a
static function pointer.
Can anyone shed some light on this?


The best you can do is to define a global or static function that uses a
global or static instance/reference/pointer to forward the call to it's
non-static member function with the passed in arguments.

The best approach depends on what your really trying to do. Perhaps you
could give more details as to what your attempting to do.

Jeff F
Jul 22 '05 #2
Jim,

Glad to see you are looking in to adding C++ to a C project. Without
going into the issues related to seperate compilers, I'm going to assume
you are using the GCC tool chain to compile both the C & C++ code. That
being the case, some example code follows after your questions. The
basic thing to take away is that you are compiling a C interface to your
C++ library, then linking your application with the g++ linker.

James Vanns wrote:
Sounds nasty doesn't it!! Well it's kinda what I need to do! I have an
external C struct (external to the C++ project/classes etc.) which is
wants a function ptr assigned to one of it's members:

struct blah {
int (func_ptr*) (int a, int b, float c);
}; etc...

However, my code is in C++ and a wish to assign a non-static member
function (I know I can do this) to this struct member! Can this be
done!? Some how with pointers or references? What about the STL's
mem_func stuff?

Can anyone shed some light on this?

Cheers

Jim


First, lets define a simple C++ class in a header file:

//simple.h A simple C++ class
//
#include <string>
#include <complex>

using namespace std;

class simple{
public:
simple();
int func1( int flags );
int func2(string s, complex<double> c);
friend simple operator+(const simple&, const simple&);
private:
int member1;
double member2;
string member3;
};

Next, lets define the class:

//simple++.C implementation file
#include "simple.h"

simple::simple(){
member1 = 0;
member2 = 0;
member3 = "This is a dummy string";
}
int simple::func1(int flags){
member1 |= flags;
return member1;
}
int simple::func2(string s, complex<double> c){
member3 = s;
member2 = c.real();
return (int)member2;
}
simple operator+(const simple& lhs, const simple& rhs){
simple tempvar;
return tempvar;
}

Ok, now lets wrap it in some C code that will be compiled with the C++
compiler, to start, we'll describe the interface in a header:

//simple_api.h C API simple class
#ifdef __cplusplus
extern "C" {
#endif
//Do everything through a pointer
typedef void* SIMPLE_HANDLE;

//The equavalent of new();
SIMPLE_HANDLE Screate();
//The equavalent of delete();
void sfree(SIMPLE_HANDLE sh);
//the first function
int sfunc1(SIMPLE_HANDLE sh, int flags);
//the second function
int sfunc2(SIMPLE_HANDLE sh, const char *s, double re, double im);
//the overloaded operator
void sadd(SIMPLE_HANDLE sh,
SIMPLE_HANDLE const lhs, SIMPLE_HANDLE const rhs);
#ifdef __cplusplus
}
#endif

Then, we'll implement it in a ".c" file:

//simple_api.c
#include "simple_api.h"
#include "simple.h"

extern "C"{
SIMPLE_HANDLE Screate(){ return new simple; }
void sfree(SIMPLE_HANDLE sh){ delete static_cast<simple*>(sh);}
int sfunc1(SIMPLE_HANDLE sh, int flags){
return static_cast<simple*>(sh)->func1(flags);
}
int sfunc2(SIMPLE_HANDLE sh, const char *s, double re, double im){
complex<double> cd(re, im);
return static_cast<simple*>(sh)->func2(s, cd);
}
void sadd(SIMPLE_HANDLE sh,
SIMPLE_HANDLE const lhs, SIMPLE_HANDLE const rhs){
*static_cast<simple*>(sh) = *static_cast<simple*>(lhs) +
*static_cast<simple*>(rhs);
}
}

And finally, we'll write an application in C which uses the C++ library
through the C code:

#include "simple_api.h"

int main(){
SIMPLE_HANDLE sh, sh2, sh3;
int flags = 1;
int retval = 0;

sh = Screate();
sh2 = Screate();
sh3 = Screate();
retval = sfunc1(sh, flags);
retval = sfunc2(sh, "Testing ...", 1, 2);
retval = sfunc1(sh2, flags);
retval = sfunc2(sh2, "Testing ...", 3, 4);
sadd(sh3, sh, sh2);
sfree(sh); /* Strictly speaking, not needed in this code example */
sfree(sh2); /* Strictly speaking, not needed in this code example */
sfree(sh3); /* Strictly speaking, not needed in this code example */
return 0;
}
Ok, all done, time to compile & link. The following log shows how my
make file did it:
[root@angus sample]# make
gcc -c sample.c
g++ -c simple_api.c
g++ -c simple++.C
g++ -O2 -o sample sample.o simple_api.o simple++.o

Jul 22 '05 #3
On 20 Jan 2004 04:49:02 -0800 in comp.lang.c++, ji**@canterbury.ac.uk
(James Vanns) was alleged to have written:
However, my code is in C++ and a wish to assign a non-static member
function (I know I can do this) to this struct member!


It's not what you don't know that hurts, it's what you know that isn't
so.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[30.2] How do I pass a pointer to member function to a signal handler,
X event callback, etc?" It is always good to check the FAQ before
posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
Jul 22 '05 #4
David Harmon wrote:
This issue is covered in Marshall Cline's C++ FAQ.
See the topic "[30.2] How do I pass a pointer to member function
to a signal handler, X event callback, etc?"
# [30.2] What is "static typing,"
and how is it similar/dissimilar to Smalltalk?
It is always good to check the FAQ before posting.
You can get the FAQ at:

http://www.parashift.com/c++-faq-lite/


Jul 22 '05 #5
E. Robert Tisdale wrote:
David Harmon wrote:
This issue is covered in Marshall Cline's C++ FAQ.
See the topic "[30.2] How do I pass a pointer to member function
to a signal handler, X event callback, etc?"

# [30.2] What is "static typing,"
and how is it similar/dissimilar to Smalltalk?
It is always good to check the FAQ before posting.
You can get the FAQ at:

http://www.parashift.com/c++-faq-lite/


Yep. The topic they wanted was 33.2.
Jul 22 '05 #6
On Tue, 20 Jan 2004 21:11:09 GMT in comp.lang.c++, red floyd
<no*****@here.dude> was alleged to have written:
Yep. The topic they wanted was 33.2.


Time for me to wget a new copy of the FAQ.

Jul 22 '05 #7
red floyd wrote:
E. Robert Tisdale wrote:
David Harmon wrote:
This issue is covered in Marshall Cline's C++ FAQ.
See the topic "[30.2] How do I pass a pointer to member function
to a signal handler, X event callback, etc?"


# [30.2] What is "static typing,"
and how is it similar/dissimilar to Smalltalk?
It is always good to check the FAQ before posting.
You can get the FAQ at:

http://www.parashift.com/c++-faq-lite/



Yep. The topic they wanted was 33.2.

Hmm... A pretty anemic example, don't you think?

Evan

Jul 22 '05 #8

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
7
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but...
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: 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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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.