Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing C struct as a C++ class

Tags:

c++

g++

wrapper

Is there a workaround for this bug in gcc?

Specifically, I think I am running into the bug when compiling a wrapper for pthread_mutex_t. The header looks like:

class DerivedClass: public pthread_mutex_t{
  public:
     DerivedClass() {}
     ~DerivedClass(){}
     DerivedClass someFunction(){}
};

The code is from a legacy system and used to compile on GCC 3.2.x but won't do on GCC 4.1.2.

... In theory I guess I could recompile everything on a back-version GCC or refactor the referencing sources to disuse the wrapper, but I want to see if there is an easier way first.

Many Thanks.

like image 802
red Avatar asked Jul 17 '26 14:07

red


1 Answers

The bug is fixed in GCC 4.6.0, but if you need it now, use composition instead:

class DerivedClass {
  public:
    pthread_mutex_t mutex;
// ...
}

If what you need is a class which can be casted to a pthread_mutex_t *, then make sure your class has no virtual functions, and put the mutex as the first element in the class; you can then convert between DerivedClass * and pthread_mutex_t * safely. Or add an operator pthread_mutex_t *().

like image 148
bdonlan Avatar answered Jul 19 '26 04:07

bdonlan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!