[fpc-pascal] Constructors & Destructors 101

Bart bartjunk64 at gmail.com
Sat Apr 4 09:15:21 CEST 2009


On 4/3/09, Doug Chamberlin <dougchamberlin at earthlink.net> wrote:
>  Essentially, yes.
>
>  However, you may create subtle, lurking bugs if you omit that call and
> later refactor your code. For example, if you later change
>
>   type
>     TFoo = class
>
>  to
>
>   type
>     TFoo = class(TSomeClass)
>
>  and TSomeClass has some important work done in it's Create constructor. If
> you properly called "inherited Create;" now that important stuff will get
> done, just the way you want it to!
>

Despite the fact that I've been programming in Pascal and Delphi for
quite some time now, I've only recently started looking into and
writing components (thanks to Lazarus!).
Most tutorials on that topic started out with TComponent decendents,
where invoking the inherited contsructor is eminent.
I've since seen several TObjet derived classes, some of them invoking
inherited Create, some not. I never understood the difference and this
made me insecure wether my own (small) TObject derived classes (I
always invoked inherited Create in my constructor) were potentially
flawed.

>  <emphasis>
>  So, the big lesson here is to stick to the proper structure even though you
> may have outside knowledge that the form you should follow is not strictly
> necessary in all cases.
>  </emphasis>

I agree.

Thanks for clearing things up to me.

Bart



More information about the fpc-pascal mailing list