I have two classes, roughly defined like this:
class Inner {
public:
bool is_first;
};
class Outer {
public:
char some_other_member;
Inner first;
Inner second;
}
I known that Inner
s only ever live inside Outer
s, and that the respective bool flag will be set to true
if and only if the respective object is the first
member, not the second
.
I am looking for a standard-compliant way of deriving a pointer to the Outer
object containing some Inner
object. Of course I could just store a pointer inside every Inner
object, but since the Inner
class is very small and I have lots of them, that seems like a waste of memory (and thus precious cache).
Obviously, the compiler should know the memory offset between first
, second
and the containing Outer
object. The question is: Is there a standard-compliant way of telling the compiler "get that offset, subtract it from the pointer to Inner
and make it an Outer
pointer"?
I know I could use casting to void
if Outer
would contain the Inner
s as base subobjects (e.g. this) - I very much feel like something similar should be possible for member subobjects?