[fpc-pascal] Questions About Constructors

Michael Van Canneyt michael at freepascal.org
Thu Mar 30 09:25:36 CEST 2017



On Thu, 30 Mar 2017, African Wild Dog wrote:

> Hello,
>
> 1 - What happens if my constructor raise an exception? Is my destructor
> automatically called?

Yes.

>
> 2 - Are the class fields automatically initialized to Default(T) just like
> in Delphi?

Yes. The're zeroed out when the memory for the class is allocated.

Just run the following program. You can add a field and print the contents
in the destructor.

{$mode objfpc} 
{$h+} 
uses sysutils;

Type
   TA = Class(TObject)
     constructor create;
     destructor destroy; override;
   end;

   Constructor TA.Create;

   begin
     Raise Exception.Create('aha');
   end;

   Destructor TA.Destroy;

   begin
     writeln('In destructor.');
   end;

var
   A : TA;

begin
   A:=TA.Create;
end.



More information about the fpc-pascal mailing list