[fpc-pascal]WoW

Matt Emson memsom at interalpha.co.uk
Sat Aug 18 13:44:06 CEST 2001


Firstly, the class you define is not a valid Object Pascal class - if FPC
compiles it, the FPC is wrong. You have to declare all variables in the
varous sections *before* methods and properties. Delphi is very strict about
this.

Secondly, you have to call a constructor to create an instance of the class
before you can use it. This is similar (but not identical) to the way in
which Turbo Pascal Objects worked when you create them on the heap.

Thirdly, 'Class' style objects follow a certain convention: Constructor
called 'Create', Destructor called 'Destroy', which you must 'Override'.
*Every* class style object inherits from 'TObject' whether you state this in
its declaration or not. You do *not* call destroy directly, there is a
method called 'free' which you must use.

Your code should look as follows:

> PROGRAM Essai;
> {$MODE OBJFPC}
> TYPE
>  a = CLASS
>  PUBLIC
>       x : Integer;
>       CONSTRUCTOR Create;
>       DESTRUCTOR Destroy; override;
>  END;
>
>  CONSTRUCTOR a.Create;
>  BEGIN
>  END;
>
>  DESTRUCTOR a.Destroy;
>  BEGIN
       inherited Destroy;
>  END;
>
> VAR
>  b : a;
> BEGIN
    b := a.create; { You must create an instance... }
>  b.x := 10;
>  write(b.x);
    b.free; { YOU MUST CALL THE DESTRUCTOR, you do this with the 'free'
method.}
> END.

Try compiling this as a 'Delphi' compatible app.

If you wanted 'stack' based objects try:

> PROGRAM Essai;
> {$MODE OBJFPC}
> TYPE
>  a = Object
>  PUBLIC
>       x : Integer;
>       CONSTRUCTOR Init;
>       DESTRUCTOR Done;
>  END;
>
>  CONSTRUCTOR a.Init;
>  BEGIN
>  END;
>
>  DESTRUCTOR a.Done;
>  BEGIN
>  END;
>
> VAR
>  b : a;
> BEGIN
>  b.x := 10;
>  write(b.x);
> END.

That second example is untested.

To the FPC team: If anything I say about Object Pascal is untrue in FPC then
I'm afraid that it's FPC that has it wrong. Delphi's implementation of the
'class' is a standard. You can't 'break' it. If you want to mess with this
declaration, call it 'FPC_CLASS' or something.

Matt




----- Original Message -----
From: "Shifted Soul" <snsii at yahoo.fr>
To: "fpc mailing list" <fpc-pascal at deadlock.et.tudelft.nl>
Sent: Friday, August 17, 2001 7:00 PM
Subject: [fpc-pascal]WoW


> Lets see this code :
>
> PROGRAM Essai;
> {$MODE OBJFPC}
> TYPE
>  a = CLASS
>      PUBLIC
>       CONSTRUCTOR a1;
>       x : Integer;
>       DESTRUCTOR a2;
>      END;
>  CONSTRUCTOR a.a1;
>  BEGIN
>  END;
>
>  DESTRUCTOR a.a2;
>  BEGIN
>  END;
>
> VAR
>  b : a;
> BEGIN
>  b.x := 10;
>  write(b.x);
> END.
>
> As I learned in the documentation, b is a pointer that is not allocated
> :
> "Remember that a class is a pointer to an object, so when you declare a
> variable of some class, the compiler just allocates a pointer, not the
> entire object."
>
> documentation : Reference guide : Classes
> then why this code is functional ?!
> This is my problem : I am using a pointer which not allocated (Am I
> using a ghost memory ???!).
> Please tell what's the problem (it is a bug ?).
> Shifted Soul
>
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal





More information about the fpc-pascal mailing list