[fpc-devel] {$M+} and forward declarations do not work
Peter Vreman
peter at freepascal.org
Tue Dec 20 08:23:07 CET 2005
> Hi
>
> I have been busy porting a large Delphi app to FP & Lazarus. This issue
> took me quite a while to find. My RTTI unit tests kept on failing for
> no obvious reason.
>
> Finally I found the issue. It is a difference between how Delphi and
> FPC handles Forward Declarations and the {$M+} directive.
>
> I have a workaround for my code, but I'm not sure which compiler handles
> this correctly and which one doesn't. I did get this info from the
> Delphi 7 help titled 'Runtime type information': "Note that if a class
> is forward declared, the first declaration of the class must be declared
> with the $M switch."
>
> This works under Delphi, but not in FPC. Puting the {$M+} around the
> forward declaration. You don't get any compiler error in FPC, but
> calling the GetPropList fuction to retrieve a list of published
> properties for example will thow an AV error at runtime. See example #1
>
> To get this to work in FPC (but then it fails in Delphi) is to put the
> {$M+} around the actual class declaration and not around the forward
> declaration. See example #2
Please submit always complete code. Now we need to guess what is wrong and
in a lot of cases we can't reproduce it with our own code like in this
situation. The code below works perfectly:
{$ifdef fpc}{$mode objfpc}{$endif}
uses
TypInfo;
type
{$M+}
TMyTestObject = class;
{$M-}
TSomeOtherClass = class(TObject)
end;
type
TMyTestObject = class(TObject)
private
FIntProp: integer;
FStringProp: string;
public
published
property StringProp: string read FStringProp write FStringProp;
property IntProp: integer read FIntProp write FIntProp;
end;
var
PropList: PPropList;
begin
writeln(GetPropList(TypeInfo(TMyTestObject),PropList));
end.
More information about the fpc-devel
mailing list