Questions About Constructors

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Questions About Constructors

African Wild Dog
Hello,

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

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


Regards

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Questions About Constructors

Mattias Gaertner
On Thu, 30 Mar 2017 03:47:59 -0300
African Wild Dog <[hidden email]> 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?

They are all initialized. I'm not sure if Default(T) is always the same
value. I guess yes.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Questions About Constructors

Michael Van Canneyt
In reply to this post by African Wild Dog


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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Questions About Constructors

African Wild Dog
2017-03-30 4:25 GMT-03:00 Michael Van Canneyt <[hidden email]>:


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.


Thanks for the clarification.
I've issued #0031619  to add these details in the documentation.


Best regards

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Questions About Constructors

Free Pascal - General mailing list

Am 31.03.2017 19:27 schrieb "African Wild Dog" <[hidden email]>:
>
> 2017-03-30 4:25 GMT-03:00 Michael Van Canneyt <[hidden email]>:
>>
>>
>>
>> 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.
>>
>
> Thanks for the clarification.
> I've issued #0031619  to add these details in the documentation.

Keep in mind however that the second behavior can be changed by overriding TObject.NewInstance (the default implementation allocates the memory of the class and zeroes it).

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...