Past few days I have been "downgrading" > 1000 filem of C++ code into C. It's been going well until now. Suddenly I'm face to face with a class...
The compiler pointed out the error first in the header file:
class foobar {
foo mutex;
public:
foobar() {
oneCreate(&mutex, NULL);
}
~foobar() {
oneDestroy(mutex);
mutex = NULL;
}
void ObtainControl() {
oneAcquire(mutex);
}
void ReleaseControl() {
oneRelease(mutex);
}
};
And of course, the C file has to take advantage of this
foobar fooey;
fooey.ObtainControl();
I don't even know where to start.... Help?
Turn foobar into a normal struct
Create your own "constructor" and "destructor" as functions that you call on that struct
etc
since C-structs can't have member functions, you can either make function pointers, or create non-member versions of those functions, ex:
and in the .C file:
There are actually compilers that compile from C++ to C. The output is not meant for human digestion, though, see How to convert C++ Code to C.
It depends on your compiler because there isn't a standard way of RAII in C. See this question and the top answer.