[Dev-C++] C++ and C function pointers
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Patrick, M. <Mic...@sp...> - 2002-08-13 20:09:22
|
Hello all,
I'm in no way a master of C++ and all its rules and regulations and I don't
think what I need to do can actually be done, but it never hurts to ask
anyway.
I have to create a simple API using a C++ library (required by management)
that interfaces with an old C library beneath it. My problem is that the
old C library has 3 or 4 function calls, each of which takes a function
pointer, to allow you to handle the results as you wish. This is fine, but
I need to take the returned values (from the function pointer call) and use
them with a bunch of my own variables currently held in a class.
From my understanding, there is no way to mask a C++ class method as a C
function (something to do with the this pointer). Do you know of any way to
overcome this?
My current work around is by using a global structure X which has variables
a, b and c. The class D configures variable a and b and the callback
function configures variable c. The class D then takes all 3 variables and
handles them with method doWork() to display the results to the user.
Typedef Struct hndlmyStruct{
Int a
Int b
Int c
} myStruct
class D{
public:
doWork(){
this.add()
};
private:
add(){
cout << X.a + X.b + X.c;
};
}
void function( int y){
X.c = 3;
}
myStruct X;
void main{
new D
X.a = 1
X.b = 2
LibraryCommand( function )
D.doWork();
}
What I would like to do is pass the method doWork() as the function pointer,
but I'm almost positive you can't do this. Another work around is if I can
somehow give the callback function access to the class D.
Is any of this possible or should I just stick with the global structure?
Hope this makes some sense, I'll clarify anything if need be.
Michael
|