RE: [Dev-C++] C++ and C function pointers
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2002-08-13 22:57:43
|
<http://run.to/noicys> -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Patrick, Michael Sent: Tuesday, August 13, 2002 11:11 PM To: 'dev...@li...' Subject: [Dev-C++] C++ and C function pointers 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? I hope i have understood what you are asking for. Below i am displaying some of what you can do in a crazy example. namespace outerspace { int nspvalue; extern "C" { // You can use these inside C code void setnspvalue(int nval) { nspvalue=nval; } int getnspvalue() { return nspvalue; } } } // Your C function delcared here extern "C" int your_c_function_declaration( int (*)(void) ); class A { int x; public: // ... A(int n):x(n) {} void setvalue(int new_value) { x=new_value; } int getvalue() const { return x; } }; int main() { using outerspace::setnspvalue; using outerspace::getnspvalue; A a(7); setnspvalue(a.getvalue()); // You can use the function outerspace::setvalue() inside a C function int y=your_c_function_declaration (&getnspvalue); a.setvalue(y); } Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net <http://www.noicys.cjb.net/> * Alternative URL: http://run.to/noicys |