Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.184 Beiträge
 
Delphi 12 Athens
 
#6

Re: OffsetOf Delphi ekvivalent?

  Alt 29. Dez 2009, 10:52
Code:
#define offsetof(struct_type, member) \
          (size_t) &(((struct_type *)0)->member)
This would presumably it look something like this in Delphi.
Delphi-Quellcode:
function OffsetOf(StructType: Type; Member: string): Integer;
  type P = ^StructType;

  begin
    //Result := Integer(@P(nil)^.[Member]) - Integer(@P(nil)^);
    //Result := Integer(@P(nil)^.[Member]) - Integer(P(nil));
    //Result := Integer(@P(nil)^.[Member]) - 0;
    Result := Integer(@P(nil)^.[Member]);
  end;
My code would look something like this.
Delphi-Quellcode:
function OffsetOf(StructType: Type; Member: string): Integer;
  var X: StructType;

  begin
    Result := Integer(@X.[Member]) - Integer(@X);
  end;
But Delphi does not support macros.

Zitat von nanix:
Code:
length = OffsetOf(member, CharData) +  sizeof(member->CharData);
length := Integer(@PMember(nil).CharData) + SizeOf(TMember.CharData);
$2B or not $2B
  Mit Zitat antworten Zitat