I have what seems to be a pretty basic question, but I just wanted to make sure.
Is there a difference between those two?
var ClassArray: array of TMyClass;
constructor TMyClass.Create;
begin
SetLength(ClassArray, Length(ClassArray)+1);
ClassArray[Length(ClassArray)-1]:=Self;
end;
begin
for i:=0 to x do
ClassArray[i].MyProcedure;
and
var PointerArray: array of Pointer;
constructor TMyClass.Create;
begin
SetLength(PointerArray, Length(PointerArray)+1);
PointerArray[Length(PointerArray)-1]:=Self;
end;
begin
for i:=0 to x do
TMyClass(PointerArray[i]).MyProcedure;
Because from the way it’s working when I play around with it, the only difference is that I cannot directly view the elements in PointerArray (as only the address is shown).
Thanks
The two versions are identical in terms of the code that the compiler generates. This is because an instance reference is implemented as a pointer.
The difference is that for the version based on pointers, the compiler does not know that the array contents are instance references. That’s why you have to cast to
TMyClassin order to be able to invoke a method, and why the debugger insight is only able to show you an address.