I am wondering if the code below is valid or not.
It executes, but compiled with -CR gives a runtime error in fpc_check_object

xfoo is a variable to store a class of TFooClass, or any inherited class. It is initialized with the inherited class TBar.
However casting tothe class of  TBar (using TBarClass) causes an error?

In the below example, of course casting wasn't needed.
But if "var xfoo : TFooBaseClass" and TFooBaseClass did not have "hello" then casting was needed (as soon as you knew, it was at least TFoo or decendant.

Martin
---

program project1;
{$mode objfpc}{$H+}
uses  Classes, sysutils;

type
 TFoo = Class
   class procedure hello; virtual;
 end;
 TFooClass = Class of TFoo;

 TBar = class(TFoo)
   class procedure hello; override;
 end;
 TBarClass = Class of TBar;

class procedure TFoo.hello;
begin
 writeln('foo');
end;

class procedure TBar.hello;
begin
 writeln('world');
end;

Var    xfoo: TFooClass;

begin
 xfoo := tfoo;
 writeln('hello');
 writeln(xfoo.ClassName);
 TBarClass(xfoo).hello;
 sleep(5000);
end.

_______________________________________________
fpc-devel maillist  -  [email protected]
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to