[fpc-pascal] Constructors & Destructors 101
Jonas Maebe
jonas.maebe at elis.ugent.be
Fri Apr 3 11:15:19 CEST 2009
On 03 Apr 2009, at 03:43, Richard Ward wrote:
> A) The documentation says that for the create constructor:
>
> (quote}
> Description: Create creates a new instance of TObject. Currently it
> does nothing. It is also not virtual, so there is in principle no
> need to call it directly.
> {unquote}
>
> What is it meant by: "no need to call [create] directly?" How do
> you invoke the constructor without calling it?
I think what is meant, is that if you create a direct subclass of
TObject, there is no need to call TObject's create constructor (e.g.,
via "inherited create;") from your own constructors. It doesn't hurt
if you do it of course, and may be good practice to account for future
situations where the parent class may change.
> ... and ... Why is create not virtual and the destroy destructor is?
Because when creating a class instance, you usually know the exact
class type of that instance (e.g., TObject.create -> you know that the
created instance type will be of the type TObject). This only changes
if you use class reference types, but if you use those you can still
declare your own virtual constructors.
Conversely, many routines that free a class instance, have no idea
about the exact type of that instance (such as FreeAndNil: all it
knows is that the instance inherits from TObject). So to make sure
that all resources allocated by those class types are properly freed,
the destructor almost has to be virtual (since otherwise, only
TObject's dummy destructor would be called by, e.g., FreeAndNil).
> B) The documentation says that for the destroy destructor:
>
> (quote}
> Description: Destroy is the destructor of TObject. It will clean up
> the memory assigned to the instance. Descendent classes should
> override destroy if they want to do additional clean-up. No other
> destructor should be implemented.
> {unquote}
>
> What is it meant by: "No other destructor should be implemented?"
It means that you should not add "destructor
my_peculiarly_named_destructor; virtual;" to your own classes. The
reason is that TObject's free method, which is used by pretty much all
code out there, is hardcoded to call "destroy". So if your destructor
is named differently, a lot of existing code will not properly destroy
instances of your class type.
> ...and... Does it do "something" while the Create constructor
> doesn't?
No, TObject's default destructor does not do anything either.
> If you had two different create constructors (for whatever reason),
> might you not also need two different destroy destructors?
No, the default destructor should always free all resources,
regardless of how the class instance was created. Otherwise, it would
also make your code more complex, because throughout the code you
would have to track how the instance was created, so that in the end
you could call the correct destructor.
> What problems might you get into if you did?
See above.
Jonas
More information about the fpc-pascal
mailing list