[fpc-devel] {$M+} and forward declarations do not work
    Graeme Geldenhuys 
    graemeg at opensoft.homeip.net
       
    Tue Dec 20 07:53:24 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
Works in Delphi, but not in FPC.
---[Example #1]---------------------------------------
type
{$M+}
   TMyTestObject = class;
{$M-}
   TSomeOtherClass = class(TObject)
   ...
   end;
   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;
------------------------------------------
Works in FPC, but doesn't in Delphi.
---[Example #2]---------------------------------------
type
   TMyTestObject = class;
   TSomeOtherClass = class(TObject)
   ...
   end;
{$M+}
   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;
{$M-}
------------------------------------------
Regards,
   - Graeme -
    
    
More information about the fpc-devel
mailing list