假设我有以下带有以下声明的自定义列表:
type
TCustomList = class(TObjectList)
private
function GetItem(AIndex: Integer): TMyObject; // virtual;
procedure SetItem(Index: Integer; AObject: TMyObject);
...
public
property Items[Index: Integer]: TMyObject read GetItem write SetItem;
procedure InsertSort();
end;
通过以下实现:
implementation
function TCustomList.GetItem(AIndex: Integer): TMyObject;
begin
Result := TMyObject(inherited Items[AIndex]);
end;
procedure TCustomList.SetItem(Index: Integer; AObject: TMyObject);
begin
inherited Items[Index]:= AObject;
end;
procedure TCustomList.InsertSort;
var
i, j: integer;
key: TMyObject;
begin
for j := 1 to self.Count - 1 do
begin
key:= self.Items[j];
i := j - 1;
while ((i >= 0) AND (self.Items[i].Compare(key)>0)) do
begin
self.Items[i+1]:= self.Items[i]; // does not WORK!!! properly.. System.Contnrs problem ??
i := i-1;
end; // while
self.Items[i+1]:= key;
end; // for
end; // procedure InsertSort
当我在 的实例集合上运行代码时TMyObject
,我得到一个无效指针操作异常。我认为,这是由于TCustomList
viaItems
属性的元素的读取和写入不佳造成的。
为什么会出现这个无效指针操作异常?