| Steve,
As I understand it, methods (i.e. member functions) of classes are
effectively static. I.e. you have exactly one copy of each regardless of the
number of class instances that you declare, even 0. This is the property that
allows you to call a member function even though you appear to have no variables
allocated of that class. Of course, this property isn't very useful, since
there are no data members to read or write!
-Kevin
|
| Ah thanks!
So if I did have data members, am I going to be able to keep any class
instances in scope anyway?
I.e., I need C++ functions within which to declare the class, and
they'll destruct again when they go out of scope, when I leave that
function.
Is that right?
Cheers,
Steve.
|
| > So if I did have data members, am I going to be able to keep any class
> instances in scope anyway?
> I.e., I need C++ functions within which to declare the class, and
> they'll destruct again when they go out of scope, when I leave that
> function.
> Is that right?
Not sure what you mean by the above. When we've needed to do something
like this, we've exported a "C image" of the object(s), and provided
C-callable routines to invoke each member function that we want to
expose, as shown below. Data members are normally accessed only by the
C++ code, but if you want to expose them you can add 'extern "C"'
functions to read and write them. The only thing to watch out for is
that, if you want your code to be reasonably portable, you have to make
sure that your C++ code doesn't have any static object declarations,
because a C main program may not construct such objects on some
platforms.
John
class CPP_CLASS {
...
public:
function1(...);
function2(...);
...
CPP_CLASS(params); // Constructor
~CPP_CLASS(); // Destructor
};
extern "C" void * create_object(params) {
CPP_CLASS * object;
object = new CPP_CLASS(params);
return (void *)object;
};
extern "C" void destroy_object(void * Cobj) {
CPP_CLASS * cpp_object;
cpp_object = (CPP_CLASS *)Cobj;
delete cpp_object;
};
extern "C" void funct1(void * Cobj, ...) {
CPP_CLASS * cpp_object;
cpp_object = (CPP_CLASS *)Cobj;
cpp_object->function1(...);
};
extern "C" void funct2(void * Cobj, ...) {
CPP_CLASS * cpp_object;
cpp_object = (CPP_CLASS *)Cobj;
cpp_object->function2(...);
};
|