[lazarus] Order of destruction in TCustomListBox

Mattias Gaertner nc-gaertnma at netcologne.de
Sun Dec 14 20:17:15 EST 2003


On Sun, 14 Dec 2003 15:09:48 +0100  Micha Nelissen <micha at neli.hopto.org>
wrote:

> Mattias Gaertner wrote:
> 
> > On Sat, 13 Dec 2003 22:22:30 +0100  Micha Nelissen
> > <micha at neli.hopto.org> wrote:
> > 
> > 
> >>Mattias Gaertner wrote:
> >>
> >>
> >>>On Sat, 13 Dec 2003 19:39:39 +0100  Micha Nelissen
> >>><micha at neli.hopto.org> wrote:
> >>>
> >>>
> >>>>Aah, the 'yucky'-ness of Object Pascal :-(.
> >>>
> >>>
> >>>What do you mean?
> >>
> >>I don't like the fact of pascal that it's possible that overriden 
> >>functions get called although their constructor hasn't run or their 
> >>destructor has run.
> > 
> > 
> > What function gets called after the destructor has run?
> 
> We need some code :-).
> 
> <code>
> program Virt;
> 
> {$mode objfpc}
> {$h+}
> 
> type
> CA = class
> private
>    procedure SomeFunc; virtual; abstract;
> public
>    constructor Create;
> end;
> 
> CB = class(CA)
> private
>    procedure SomeFunc; override;
> public
>    constructor Create;
> end;
> 
> constructor CA.Create;
> begin
>    SomeFunc;
> end;
> 
> constructor CB.Create;
> begin
>    inherited;
>    writeln('Constructor');
> end;
> 
> procedure CB.SomeFunc;
> begin
>    writeln('CB.SomeFunc call');
> end;
> 
> var
>    VB: CB;
> begin
>    VB := CB.Create;
>    VB.Free;
> end.
> </code>
> 
> <output>
> CB.SomeFunc call
> Constructor
> </output>
> 
> Which is not logical, IMHO.

It is just a matter of definition. Although it would be interesting how you
imagine a more logical alternative.

An object has always the same type, from the beginning till the end. That
means from the very first line in any of its constructors till the last line
in any destructor. For example: When a TControl comes down to the TObject
constructor, it is still/already a TControl. For instance it does not 
create first a TObject, then a TPersistence, then a TComponent, then ... .
This is OOP and is independent of object pascal. 
Virtual Methods should always work the same. If called inside the
constructor they do what they always do. For example: To forbid calling them
inside the constructor of TComponent would mean: This is not yet a TControl,
so you can't use the overriden methods - which is wrong. Only some variables
are not yet initialized. 


Mattias






More information about the Lazarus mailing list