Without a pre-processor or a built-in function, there's no way to do it quite as cleanly as the offsetof
macro. The way that offsetof
is able to do it so cleanly is that the pre-processor does the work. In fact some compilers implement it as a built-in, but that's beside the point. Delphi has no pre-processor, and no built-in offsetof
.
The cleanest solution I know is like this:
NativeUInt(@TMyRecord(nil^).MyField)
But that is nothing like as clean as
offsetof(struct MyStruct, MyField)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…