That offsetof-like trick allows classes where they must be disallowed and disallows where they could be allowed.
Previously on “Offsetof-like Trick”: #39665 (closed), #39666 (closed).
This code erroneously compiles, as if Indirect was a record (with extra VMT pointer).
type
Indirect = class
hasNoOffsetInMyObj: int32;
end;
PMyObj = ^MyObj;
MyObj = record
c: Indirect;
end;
const
WrongOffset = PtrUint(@PMyObj(nil)^.c.hasNoOffsetInMyObj); // 8 on x64
begin
end.
BUT, while we at it, I want to make a feature request, too.
Allow classes (and arrays if #39666 (closed) will be fixed) as a top-level entity. Presently, this does not compile:
type
SomeClass = class
field: int32;
end;
const
FieldOffset = PtrUint(@SomeClass(nil).field);
begin
end.
but looking at how PtrUint(@PMyObj(nil)^.c.hasNoOffsetInMyObj) essentially gave the offset of hasNoOffsetInMyObj in Indirect instance, there are no obstacles to do it in valid scenarios.
Edited by Rika