[fpc-devel] -CR (method checking) and casting of classes

Martin lazarus at mfriebe.de
Sun Aug 2 04:17:56 CEST 2009


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.




More information about the fpc-devel mailing list