7

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?

Rasman
  • 5,349
  • 1
  • 25
  • 38
  • 1
    Isn't OO in C just `ObtainContol(&fooey);`? You an even call the parameter `this`. – Bo Persson May 03 '11 at 17:01
  • 2
    @Bo But that isn't going to magically simulate RAII, which this class (and presumably others) is using. –  May 03 '11 at 17:04
  • @unapersson - Right, I focused on the second line of the code sample. The upside is that you don't have any exceptions either, so RAII is less important. "Just" add construct_foobar and destruct_foobar and call them at the right spots. Now remember why I like C++ better! – Bo Persson May 03 '11 at 17:12
  • 5
    Why would you downgrade from C++ to C? What is the reason behind that decision? – David Rodríguez - dribeas May 03 '11 at 17:27

4 Answers4

12

Turn foobar into a normal struct

struct foobar {
    goo mutex;
};

Create your own "constructor" and "destructor" as functions that you call on that struct

void InitFoobar(foobar* foo)
{
   oneCreate(&foo->mutex);
}

void FreeFoobar(foobar* foo)
{
   oneDestroy(foo->mutex);
}

struct foobar fooStruct;
InitFoobar(&fooStruct);
// ..
FreeFoobar(&fooStruct);

etc

rtn
  • 127,556
  • 20
  • 111
  • 121
5

since C-structs can't have member functions, you can either make function pointers, or create non-member versions of those functions, ex:

struct foobar {
    foo mutex;
};

Construct_foobar(foobar* fooey) {
    oneCreate(&fooey->mutex, NULL);
}
Destroy_foobar(foobar* fooey) {
    oneDestroy(fooey->mutex);
    fooey->mutex = NULL;
}
void ObtainControl(foobar* fooey) {
    oneAcquire(fooey->mutex);
}
void ReleaseControl(foobar* fooey) {
    oneRelease(fooey->mutex);
}

and in the .C file:

foobar fooey;
construct_foobar( &fooey );
ObtainControl( &fooey );
quamrana
  • 37,849
  • 12
  • 53
  • 71
Dan F
  • 17,654
  • 5
  • 72
  • 110
  • 3
    You missed the final part: find every point at which `fooey` goes out of scope, and add calls to `destroy_foobar`. – Mike Seymour May 03 '11 at 17:14
  • yeah, that is true as well, you have to worry about automatic destruction on scoped variables – Dan F May 03 '11 at 17:15
0

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.

Community
  • 1
  • 1
Tilman Vogel
  • 9,337
  • 4
  • 33
  • 32
  • thanks for the reference, problem is that the final code may very well need to be scrutinized (regulations...) – Rasman May 03 '11 at 17:52
0

It depends on your compiler because there isn't a standard way of RAII in C. See this question and the top answer.

Community
  • 1
  • 1
Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180