16 is the address we're assigning to the pointer, which lets us calculate the offset of the specified member. The address of a pointer is just a number, so we can abuse this fact in order to get information about our structures/classes.
Say we have a struct:
struct point {
//Assuming 32-bit integer sizes.
//For 64-bit integersizes, 0x0, 0x8, 0x10 for the integer offsets
int x; //Offset 0x0
int y; //Offset 0x4
int z; //Offset 0x8
}; static_assert(sizeof(point) == 12 /* or 0xC in hex */);
We use the macro:
OFFSETOF_MEMBER(point, y);
Expanding the macro, we get:
(reinterpret_cast<uintptr_t>(&reinterpret_cast<point*>(16)->y) - static_cast<uintptr_t>(16u)
Another way of expressing the reinterpret_cast<point*>(16)->y
could be had like so: point * myPt = 16u;
we know 16 is not a valid address, but the compiler doesn't, and so long as we don't try to read the address we're pointing to, we're okay.
Next, we can simplify all of &reinterpret_cast<point*>(16)->y
to: &myPt->y
. We know from above that y is @ offset 0x4, and since myPt is 16: 16 + 0x4 = 20
Then we have reinterpret_cast<uintptr_t>(20u) - static_cast<uintptr_t(16u)
or 20 - 16
, which gives us the offset of y, that is, 0x4.