[Gambas-user] Constructor and destructor of a class

Bruce Bruen bbruen at ...2308...
Sat Jun 25 12:42:22 CEST 2011


On 25/06/11 14:19, Demosthenes Koptsis wrote:
> Hi all,
>
> i saw that the order of calling special methods of a class is:
>
> _init
> _new
> _free
> _exit
>
> the _new method is the constructor and _free the destructor of the
> class?
>
>
>    
Yes, but it would be better to say
     _new is a method called when an instance of a class (i.e an object) 
is created
     _free is a method called when an instance of a class is about to be 
removed.

Similarly,
     _init is a method called when the class has just been loaded
     _free is a method called just before a class is ??? unloaded ??? 
Not sure, have never found a use for one.

_init and _free are static methods, they act on the class and can not 
affect any dynamic features that are associated with an instance of the 
class.

_new and _free act on the dynamic features of a class instance.

The _new and _free methods are invoked in seniority order,  in other 
words if class C inherits class B inherits Class A then when an object 
of type C is created, the _new methods are invoked in the following 
order: A._new, B._new, C._new.  This is very important.  Also, the way 
that the _new methods "consume" parameters is extremely important.  
There is information on this in the help files.

hth
bruce




More information about the User mailing list