Converting a C++ class to a C struct (and beyond)

11,204

Solution 1

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

Solution 2

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 );
Share:
11,204
Rasman
Author by

Rasman

Medical and Engineering background. Trying to build a research career.

Updated on June 17, 2022

Comments

  • Rasman
    Rasman almost 2 years

    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?

    • Bo Persson
      Bo Persson about 13 years
      Isn't OO in C just ObtainContol(&fooey);? You an even call the parameter this.
    • Admin
      Admin about 13 years
      @Bo But that isn't going to magically simulate RAII, which this class (and presumably others) is using.
    • Bo Persson
      Bo Persson about 13 years
      @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!
    • David Rodríguez - dribeas
      David Rodríguez - dribeas about 13 years
      Why would you downgrade from C++ to C? What is the reason behind that decision?
  • Mike Seymour
    Mike Seymour about 13 years
    You missed the final part: find every point at which fooey goes out of scope, and add calls to destroy_foobar.
  • Dan F
    Dan F about 13 years
    yeah, that is true as well, you have to worry about automatic destruction on scoped variables
  • Rasman
    Rasman about 13 years
    thanks for the reference, problem is that the final code may very well need to be scrutinized (regulations...)