[fpc-pascal] parent class as a parameter type
Sven Barth
pascaldragon at googlemail.com
Thu Nov 17 20:38:42 CET 2011
On 17.11.2011 19:07, Mattias Gaertner wrote:
> On Thu, 17 Nov 2011 15:15:46 +0100
> Sven Barth<pascaldragon at googlemail.com> wrote:
>>
>> Emagine the following code:
>>
>> procedure Foo(var aList: TStrings);
>> begin
>> aList.Free;
>> aList := TStrings.Create;
>> end;
>>
>> var
>> l: TStringList;
>> begin
>> l := TStringList.Create;
>> Foo(l);
>> l.Sort;
>> end.
>>
>> When you run this code you will have a big problem. Your "l" variable is
>> a TStringList, but after calling Foo your variable holds a TStrings
>> instance (which is lower in the hierarchy than TStringList)! If you now
>> call Sort you will get an access violation if you're lucky or some
>> strange error if not, because Sort is introduced by TStringList. Thus
>> you call into "nothing" when executing this on a TStrings.
>>
>> Is the reason why this is forbidden more clear now?
>
> The l.Sort is the important line, that is missing in the wiki example.
>
> Maybe this example can replace the example in the wiki?
I have extended the example in the wiki a bit in the hope to clarify the
seriousness of this issue.
Regards,
Sven
More information about the fpc-pascal
mailing list