我看了下,这个Pointer(Result) := PPointer(Self)^应该等同于 Result := TClass(PPointer(Self)^);
蓝叶菱:你的说法不对;
以下是测试代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
if ClassType = TClass(PPointer(Self)^) then begin
Caption := TClass(PPointer(Self)^).ClassName;
end;
if Pointer(ClassType) = PPointer(Self)^ then begin
Caption := Caption + '-' + TClass(PPointer(Self)^).ClassName;
end;
end;
其实Delphi中有很多这样的写法,如同在这个System.pas里:
function InvokeImplGetter(Self: TObject
ImplGetter: Cardinal): IInterface;
{$IFDEF PUREPASCAL}
var
M: function: IInterface of object;
begin
TMethod(M).Data := Self;
case ImplGetter of
$FF000000..$FFFFFFFF: // Field
Result := IInterface(Pointer(Cardinal(Self) + (ImplGetter and $00FFFFFF)));
$FE000000..$FEFFFFFF: // virtual method
begin
// sign extend vmt slot offset = smallint cast
TMethod(M).Code := PPointer(Integer(Self) + SmallInt(ImplGetter))^;
Result := M;
end;
else // static method
TMethod(M).Code := Pointer(ImplGetter);
Result := M;
end;
end;
实际上就是转换类型,以便类型相同而通过编译,一般的做法是左值
=前面的)不应该用类型转换的,但Delphi编译器支持啊,所以就能这样用了;
其他的开发工具或语言似乎很少支持或使用这样的用法.