I want to, basically, inherit a C struct in C++ (while literally). I have:
struct foo { // C-side definition
int sz;
/* whatever */
// no virtual destructor, special mechanism
};
class cxx_class {
/* something here */
// no virtual destructor, no need
};
class derived : public foo /*, public cxx_class */ {
/* some other stuff here */
};
derived
foo*
foo
derived
reinterpret_cast
foo
derived
static_cast
static_cast
reinterpret_cast
derived
instances (in form offoo*
) will be passed back to the external library which only knows and uses thefoo
part ofderived
(of course).
If your functions take a foo*
, it doesn't matter. Even if constructing your types in such a way that foo
is not placed at the beginning of derived
, the conversion from derived*
to foo*
happens in the C++ code which does know about the type layout. The external function then doesn't need to worry about it.