Say we have an object a like this:
struct B {
void access_a() {
A* a = reinterpret_cast<A*>(reinterpret_cast<char*>(this)
- offsetof(A, b));
// is accessing a here well defined, assuming this object
// is one that is inside struct A?
}
};
struct A {
/* one or more standard layout types before b */
B b;
} a;
a.b.access_a();
Theoretically we have indeed a pointer that points to an instance of A, and at least under certain circumstances this kind of (ugly) code seems to do what's intended. Is it well defined though? Can the compiler assume that accessing one member of an object won't alter the others (if the accessed object itself contains no pointers to the parent), or do all the child objects belong in some sort of shared mutable group?