[fpc-pascal] Generic (TFPGObjectList) EInvalidCast

Vojtěch Čihák vojtech.cihak at atlas.cz
Tue Dec 11 22:22:14 CET 2018


I forgot: FreePascal 3.3.1 r.40507
 
V.
______________________________________________________________
> Od: "Vojtěch Čihák" <vojtech.cihak at atlas.cz>
> Komu: "FPC-Pascal users discussions" <fpc-pascal at lists.freepascal.org>
> Datum: 11.12.2018 22:19
> Předmět: [fpc-pascal] Generic (TFPGObjectList) EInvalidCast
>
Hi,
  
 why this code gives EInvalidCast at runtime (but compiles well):w
  
 program Project1;
 {$mode objfpc}{$H+}
  
 uses
   {$IFDEF UNIX}{$IFDEF UseCThreads}
   cthreads,
   {$ENDIF}{$ENDIF}
   Classes, fgl
   { you can add units after this };
  
 {$R *.res}
 type
   TMyClass = class
     procedure Output1;
   end;
  
   TMyList = class (specialize TFPGObjectList<TMyClass>)
  
   end;
  
   TMyDescClass = class(TMyClass)
     procedure Output2;
   end;
  
 var MyList: TMyList;
     MyClass: TMyClass;
  
 procedure TMyClass.Output1;
 begin
   WriteLn('Output1');
 end;
  
 procedure TMyDescClass.Output2;
 begin
   WriteLn('Output2');
 end;
  
 begin
   MyList:=TMyList.Create(True);
   MyClass:=TMyClass.Create;
   MyList.Add(MyClass);
   MyList[0].Output1;
   TMyDescClass(MyList[0]);  // EInvalidCast
   MyList.Free;
 end.
  
 Thanks for help,
  
 V.
 _______________________________________________
 fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
 http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal <http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20181211/5bec42c9/attachment.html>


More information about the fpc-pascal mailing list