If a B
is always contained within an A
, then it might be much cleaner to add a back reference to B's parent inside A. If that is impractical for whatever reason, then offsetof
will clean it up a little, but otherwise the approach is valid, if somewhat c-ish.
void g(B*b){
A* a = (A*)(((char*)b)-offsetof(class A,b));
//Work with a
}