From: Benjamin R. <Ben...@ep...> - 2003-12-04 14:26:28
|
Hi Jeremy, "Jeremy Godfrey" <jn...@nt...> writes: > I call a class method by passing a void* object ptr as the first > argument to a C function which makes the actual call on the C++ > object, remembering to catch all exceptions. It's not pretty since > it involves reinterpret_casts so the user app could pass a wrong ptr > type, but it works well, most languages support calling into a C DLL > and it is portable. You don't need "void*", you can declare the classes as struct forward declarations. In C++ "class" and "struct" only differ in their default access (private vs. public), which is only important in the actual definition, otherwise they are the same thing. Example: >>>>>>>> ABC.hpp (C++ header) class ABC { . . . }; <<<<<<<< >>>>>>>> ABC.h (C wrappers header) /* Forward declaration of class ABC, never actually filled in C. */ struct ABC; struct ABC * newABC( void ); void deleteABC( struct ABC * obj ); <<<<<<<< >>>>>>>> ABC-wrapper.cpp (C wrappers implementation) #include "ABC.hpp" extern "C" { #include "ABC.h" } extern "C" struct ABC * newABC( void ) { return new ABC; } extern "C" void deleteABC( struct ABC * obj ) { delete obj; } <<<<<<<< benny |