[fpc-pascal] Constructors & Destructors 101

Jonas Maebe jonas.maebe at elis.ugent.be
Fri Apr 3 11:03:54 CEST 2009


On 03 Apr 2009, at 03:43, Richard Ward wrote:

> C)	Just out of curiosity, am wondering why FreeAndNil is global  
> procedure instead of a method/destructor of TObject.

If you have:

var
   c: tsomeclass;
begin
   c:=c.tsomeclass.create;
   c.freeandnil
end;

then freeandnil would get, as first (hidden) parameter, the self  
pointer. However, this self pointer is a value parameter. So it would  
only contain the value of "c", not its address. Hence, there is no way  
to set "c" to nil inside the freeandnil method (while setting the  
instance variable to nil is the entire reason for using freeandnil).


Jonas



More information about the fpc-pascal mailing list