Class cast



Class cast
Pas2JS pascal source code
type TBase = class end; type TChild = class(TBase) end; { main.pas } var o := TObject.Create; c := TChild.Create; Begin Writeln(TObject(o).ClassName); Writeln(TObject(c).ClassName); Writeln((o as TObject).ClassName); Writeln((c as TObject).ClassName); try Writeln(TChild(c).ClassName); Writeln(TChild(o).ClassName); except on E: Exception do Writeln(E.Message); end; try Writeln((c as TChild).ClassName); Writeln((o as TChild).ClassName); except on E: Exception do Writeln(E.Message); end; { ### CONSOLE OUTPUTS ### TObject TChild TObject TChild TChild Cannot cast instance of type "TObject" to class "TChild" TChild Cannot cast instance of type "TObject" to class "TChild" }