[fpc-pascal]about create method
Full_Name
memsom at post.interalpha.co.uk
Fri Oct 12 11:22:57 CEST 2001
> > It's just a simple question.
> > Since every class is derived from tObject, when I implement a
> Constructor in
> > my class I have to call "inherited create;"
> > or is not necesary?
> >
Consider the following scenario:
TBase = class(TObject)
constructor Create(aParam: integer); virtual;
end;
TParent = class(TBase)
constructor Create(aParam: integer); override;
end;
TChild = class(TParent)
constructor Create(aParam: integer); override;
end;
If you do not call 'inherited create(aParam);' in TParent, you will break the
inheritence chain. TChild will never be able to be fully initialized as per
TBase. If you wish to have this, then do it, but I would advize against it.
> For the constructor not really (TObject.Create is not virtual, and
> empty),
In fact it's a good idea to make your constructor 'virtual' in all classes that
inherit from TObject. This saves problems further down the inheritence tree.
> but for the destructor it is absolutely necessary.
Vital. Your application may well acess violate or do nasty things when exiting
if the inherited destroy is not called.
> But it is good practice always to call the inherited
> constructor/destructor.
You can write conditional code that only calls the inherited version in certain
circumstances, and also vary the position of the inherited call in the
Constructor (but never the destructor.. if you do, further calls will probably
fail due to your object already being freed.)
--
"Computer games don't affect kids; I mean if Pac-Man affected us as kids,
we'd all be running around in darkened rooms, munching magic pills and
listening to repetitive electronic music."
Kristian Wilson,
Nintendo, Inc, 1989
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d? s+++:+ a- C++ UL+ P L++ E---- W- N+ o+ K- w
O- M V PS PE-- Y PGP- t- 5-- X- R- tv+ b+ DI++ D+
G e++ h--- r+++ y+++
------END GEEK CODE BLOCK------
More information about the fpc-pascal
mailing list