gpt4 book ai didi

delphi - 为什么 Delphi 7 不允许扩展类型的 TList

转载 作者:行者123 更新时间:2023-12-03 18:56:20 24 4
gpt4 key购买 nike

我已经创建了几个简单的列表(以及整数列表和颜色列表),但是当我尝试制作一个“扩展”列表时,它说无效类型转换,即使我对之前的 2 个列表使用了类似的类型转换(它抛出无论我在哪里使用 Extended() 类型转换都会出错。

Type
TAExtList = Class(TObject)
Private
FList: TList;
Procedure SetExt(Index: Integer; Value: Extended);
Function GetCnt: Integer;
Function GetExt(Index: Integer): Extended;
Public
Constructor Create;
Destructor Destroy; Override;
Function Add(Value: Extended): Integer;
Function Insert(Index: Integer; Value: Extended): Integer;
Procedure Delete(Index: Integer);
Procedure Clear;
Function IndexOf(Value: Extended): Integer;
Property Count: Integer Read GetCnt;
Property Extendeds[Index: Integer]: Extended Read GetExt Write SetExt; Default;
End;

Function TAExtList.Add(Value: Extended): Integer;
Begin
Result := FList.Add(Pointer(Value));
End;

最佳答案

两者都是 IntegerTColorPointer 具有相同的大小(4 个字节)在 Delphi 7 中,这就是为什么可以进行显式转换的原因。

docWiki :

Variable Typecasts
You can cast any variable to any type, provided their sizes are the same and you do not mix integers with reals.

但是Extended是真实的,它的大小是 10 个字节,你不能将它转换为 Pointer .而且,没有足够的地方放置它。

附言请注意,新的 Delphi 版本包含相当方便的工具 - 泛型 - 只需定义和创建 TList<Extended> .

关于delphi - 为什么 Delphi 7 不允许扩展类型的 TList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44683319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com