[fpc-pascal] Constructors & Destructors 101

leledumbo leledumbo_cool at yahoo.co.id
Fri Apr 3 10:58:55 CEST 2009


> What is it meant by: "no need to call [create] directly?"  How do you  
> invoke the constructor without calling it?  ... and ... Why is create  
> not virtual and the destroy destructor is?
Often in other OOP language, you need to call parent constructor before
doing anything else. In OP case,  you don't since it doesn't do anything.
Create isn't virtual because, as stated before, it doesn't do anything
therefore no relation with descendant constructor(s) needed. Destroy is
because it does "soemthing" (read below).

> What is it meant by: "No other destructor should be  
> implemented?"  ...and...  Does it do "something" while the Create  
> constructor doesn't?
No other destructor should be implemented if you don't need to do any
additional cleanup. It does "something", that is freeing memory assigned for
the instance while create doesn't do anything.

> If you had two different create constructors (for whatever reason),  
> might you not also need two different destroy destructors?   What  
> problems might you get into if you did?
Only if it needs two different way to cleanup. No problems if you did, I
often have overloaded constructors in classes I made (which of course only
calls the one with most arguments).

> Just out of curiosity, am wondering why FreeAndNil is global  
> procedure instead of a method/destructor of TObject.   I am guessing  
> it is for compatibility with Delphi which may or may not have a reason?
It is indeed for Delphi compatibility. Other possible reason: It can be used
by non-class types (i.e. Pointers) that do heap allocation.
-- 
View this message in context: http://www.nabble.com/Constructors---Destructors-101-tp22860474p22864567.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.




More information about the fpc-pascal mailing list