[fpc-pascal] about dynamic array
cobines at gmail.com
Fri May 7 16:10:37 CEST 2010
2010/5/7 spir ☣ <denis.spir at gmail.com>:
> I cannot directly "fish" and use the pointer's target. Seems I always need to pass _via_ the pointer, then all is fine. It's not really annoying. But I don't understand the difference: in code, both versions are indeed strictly equivalent. I guess there's an implicit (pointer?) conversion somewhere?
> Are there cases where the pointer's mediation is not needed?
There is an implicit conversion when you assign p := l, which is
PInteger := Pointer.
TFPList uses untyped Pointer for storage. You would need to make a
descendant class of TFPList and change methods to use PInteger (at
least for Items property and Get, Put methods). Then use this new
class specialized for storing PIntegers instead of TFPList.
Or take a look at generics in FPC. There is a generic class TFPGList
defined in unit "fgl". Use it like this:
TPIntegerList = specialize TFPGList<PInteger>;
l : TPIntegerList;
i : Integer;
p : ^Integer;
l := TPIntegerList.Create;
i := 1 ; new(p) ; p^ := i; l.Add(p);
p := l ; writeln(p^);
writeln(l^); // no error
More information about the fpc-pascal