Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2009-08

-CR (method checking) and casting of classes

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin

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.

Reply by email

Jonas Maebe
to Martin
Martin wrote
I am wondering if the code below is valid or not.

It is not.

Show quoted text 5 lines
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.

No, it is initialised with tfoo in your test program:

begin xfoo := tfoo;
However casting tothe class of TBar (using TBarClass) causes an error?

If you initialise xfoo with tbar, the casting works fine.

Jonas

Reply by email