[fpc-pascal] Object constructor

Michael Van Canneyt michael at freepascal.org
Tue Sep 17 09:01:47 CEST 2013



On Tue, 17 Sep 2013, Xiangrong Fang wrote:

> Hi All,
> 
> Could anyone please explain this:
>
>   5.4 Constructors and destructors
> 
> As can be seen in the syntax diagram for an object declaration, Free Pascal supports constructors and destructors. The programmer is responsible for calling the constructor
> and the destructor explicitly when using objects.
> 
> My question is, since the programmer is responsible for explicitly call object constructors, then why do we need constructors at all?  In another word, what's the difference
> between an object constructor and an object method?

Using the constructor tells the compiler that it should allocate memory on the heap for a new instance,
and return a method to this new memory. There is then an implicit return value for the 
constructor, and this is stored in the variable to which you assign the result of the constructor.

Similarly, the compiler knows that the memory allocated by the constructor must be freed again in the destructor.

Michael.


More information about the fpc-pascal mailing list