[fpc-pascal]Polymorphism of class field

Patrick O'Leary gtg086a at prism.gatech.edu
Thu Aug 30 21:49:35 CEST 2001


> > You can't.
>
>Although it might get messy, surely he could defire a variant record? 
>Something
>like:
>
>type
>   TStackElemType = (stNull, stString, stReal);
>
>   TStackElem = record
>     ElemType: TStackElemType;
>     case tag: integer of
>       0: (RealResult: Real);
>       1: (StringResult: String[255]);
>   end;
>
>   PStackelem = ^TStackElem;
>
>   procedure setStringElemData(st: string); //sets Elemtype to 'stReal'
>   procedure setRealElemData(re: real); //sets Elemtype to 'stString'
>   procedure CrearElem; //sets Elemtype to 'stNull'

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.

<snip>
>I think a more worrying thing is your following class:
>
> > > type
> > >
> > >          TStackElem = class
> > >                  public
> > >                  constructor Init; virtual;
> > >                  destructor Done; virtual;
> > >                  Prolog : TRPLObjectType;
> > >                  Next : TStackElem;
> > >                  function Decomp : string; virtual; abstract;
> > >          end;
>This class will never call 'Done'. Not in the Delphi sense of the word. 
>Try the
>following:

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.  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*:

var
         StackElement : TStackElem;
begin
         StackElement := TStackReal.Init;
         StackElement.Destroy;
end.

Am I correct in that assumption?  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?

In simple tests, everything seems to be working properly.  Heaptrc reports 
no unfreed memory.  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.

--
Patrick O'Leary
patrick.oleary at resnet.gatech.edu





More information about the fpc-pascal mailing list