[fpc-pascal]Polymorphism of class field

Matt Emson memsom at interalpha.co.uk
Mon Sep 3 21:00:58 CEST 2001


>Yeah, I tried that--major, major problems with memory leaks, though.  Not 
>to mention, the implementation wasn't nearly as clean as it would be with 
>classes.

It's nasty, but it the way at least some of the VCL works. Because Pascal is 
a strongly typed language, there's no way round it.

><snip>
>I probably should have looked up the definition of TObject in the docs 
>before I started, but now that I have done so, I'm still confused.  

It's not as straight forward as it could be :-(

>To destroy an instance of a class, I should call its destructor (which I've 
>changed to Destoy, as you said I should).  Thus the following should work 
>*and is the proper way of doing this*:

You must call 'Free'. There is a special method called 'Free' that through 
some 'magic' calls 'Destroy'.. don't ask why.. it was the way Delphi was 
implemented. I'm sure somebody can come up with a reason.

>var
>         StackElement : TStackElem;
>begin
>         StackElement := TStackReal.Init;
>         StackElement.Destroy;
>end.
>
>Am I correct in that assumption?  

No. That should be:

StackElement.Free;

It seems to work in FPC with 'Destroy', but Delphi would barf at that point.

>Also, looking at the docs, I see that 
>TObject defines a constructor, Create.  Should I use that name instead of 
>Init and override it, as I have done with Destroy?

You can call your constructor whatever you like - it doesn't matter. 'Create' 
is the convention, and what most people will expect. You can 'get away' with 
not overriding your constructor if you call it create, and just marking it 
virtual. The Tobject class defines no content for the 'Create' method.

e.g.:

TStackElement = class(TObject)
  {fields}
  constructor Create; virtual;
  {methods}
  {properties}
end;

>In simple tests, everything seems to be working properly.  Heaptrc reports 
>no unfreed memory.  

FPC lets you get away with it, though strictly 'free' is what you're after 
for 100% Delphi compatibility. 

>If there is any other good documentation on classes 
>lying around (don't bother searching for it if you don't know where it is, 
>I plan on doing that myself), let me know.  I think I need the 
>help!  Thanks for your assistance.

Any document that describes Delphi and Object Pascal will give you a good 
start. Thake a look at Marco Cantu's site.. I believe he has some free 
downloads (www.marcocantu.com iirc)

Sorry it's a little later, I dumped (more or less) Windows at the weekend, 
and so I'm still playing catch up. BeOS rocks ;-)

Matt
-- 
---
Don't have BeOS? Get it! - http://free.be.com/
Wanna keep BeOS alive? Save BeOS - 
http://www.BeFAQs.com/save/saveform.html

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d? s+++:+ a- C++ UL+ P L++ E---- W- N+ o+ K- w 
O- M V PS PE-- Y PGP- t- 5-- X- R- tv+ b+ DI++ D+ 
G e++ h--- r+++ y+++ 
------END GEEK CODE BLOCK------





More information about the fpc-pascal mailing list