FreePascal Information Logo Friend of FreePascal Compiler Title
Articles with Feedback, FPC News Library, PDF Collection, Mail Lists, Books, Newsgroups, IRC Open online discussion areas Research and Tutorials Tools, Compilers and Utilities Blurbs about us, advertising, etc.
Welcome to the FoFPC Pascal Language DESTRUCTOR

destructor


     The Destructor keyword defines a destructor procedure Destroy for a class.

     When freeing an object, the Destructor is called. This allows the object to free any storage or other volatile resources it has acquired.

     The Name for the destructor is normally destroy, but is not restricted to this. However, it is very wise to keep to this naming.

     The Override directive must be specified since we are overriding the virtual TObject destroy method.

Example

    Download Source IconDownload This Source for Free Pascal
{$MODE DELPHI} // Enable "Class"
{$M+} // Enabled "Published"
type
  // String holder record
  TString = string[10];

  // Define a container class
  TWords = class
  private
    wordCount  : Integer;
    wordsStart : Pointer;
    function Get(Index: Integer): string;
  public
    property GetWord[Index : Integer] : string read Get;
    constructor Create(count : integer);
    Destructor  Destroy; override;
  published
  end;

var
  words : TWords;

// TWords constructor - build the word array
constructor TWords.Create(count: integer);
var
  i : Integer;
  wordsList  : ^TString;
  ws:String;

begin
  // Get storage for 'count' strings
  GetMem(wordsStart, count*SizeOf(TString));

  // Fill out this list
  wordsList := wordsStart;
  wordCount := count;

  for i := 1 to count do begin
    str(i,ws);
    wordsList^ := 'Word '+Ws;
    Inc(wordsList);
  end;
end;

// TWords destructor - release storage
destructor TWords.Destroy;
begin
  // Release memory, if obtained
  if wordsStart <> nil then FreeMem(wordsStart);

  // Always call the parent destructor after running your own code
  inherited;
end;

// GetWord property read function
function TWords.Get(Index: Integer): string;
var
  wordsList  : ^TString;

begin
  // Read the word at the given index, if in range
  if (Index >= 1) and (Index <= wordCount) then begin
    wordsList := wordsStart;
    Inc(wordsList, Index-1);
    Result := wordsList^;
  end;
end;

Begin
  // Create a TWords object
  words := TWords.Create(4);

  // Now show the 2nd word in this object
  Writeln('2nd word = ',words.GetWord[2]);
end.

Output

2nd word = Word 2

See Also

Class, Constructor, Function, Inherited, Object, Procedure, TObject.
 Links and Products we find useful



ButtonGenerator.com
Valid XHTML 1.0 Transitional Internet Map
Programmer's Heaven
grat-i-fi-ca-tion - noun
the state of being gratified; great satisfaction.


"If you have a testimonial you would like to share, feel free to email me directly at ozznixon (at) (gmail.com)"

Your Web Master
Friends-Of-FPC.org
Locations of visitors to this page world map hits counter
Copyright 2009 by 3F, LLC. All rights reserved. Worldwide.
Your request was processed by server #2 in 0.002899 secs.

sponsor
This sponsor helps us with our documentation