Everything will be fine. The constructor doesnt actually do the allocation
of the memory - if it did that, you'd just need a class method, not an
explicit constructor. Instead, when a constructor is called on a class
reference the Delphi runtime does the stuff to allocate the right amount of
space for the object. In the case of your class A, it allocates enough space
for sizeof(A), which will include X, your pointer to a stringlist.
So you can see that you don't even need to call the inherited constructor to
get memory allocated for your object. Why would you want to call the
inherited constructor? The reason is that it may be initialising inherited
parts of the object for you. For instance, the constructor for TComponent
calls the InsertComponent method of the AOwner parameter to insert the newly
created component into its parent.
Its good form to call inherited Create unless you have a good reason not
too, as you may later change the heirachy to descend from an object with a
constructor that does something important.
Cheers,
  Andrew Cooke.

> -----Original Message-----
> From: Patrick Dunford [SMTP:[EMAIL PROTECTED]]
> Sent: Tuesday, August 24, 1999 12:08 AM
> To:   Multiple recipients of list delphi
> Subject:      RE: [DUG]:  Override
> 
> So what will happen in my code example (I am now using TPersistent and
> have
> overridden its Assign method, incidentally)
> Because that is the actual code I am using. I need the constructor to
> create
> X or does this happen anyway?
> 
> snip
> 
> > > Let's say I have a class declared as follows
> > >
> > > type A =  class(TObject)
> > >  X: TStringList
> > >  constructor Create; override;
> > >  destructor Destroy; override;
> > > end;
> > >
> > > constructor A.Create;
> > > begin
> > >        inherited Create;
> > >       X:=TStringList.Create;
> > > end;
> > >
> > > destructor A.Destroy;
> > > begin
> > >         X.Free;
> > >         inherited Destroy;
> > > end;
> > >
> > > Now I know that the default constructor for TObject will not
> > > allocate space
> > > to X so it seems logical to override the default constructor
> > with my own,
> > > and call the inherited constructor within that. Ditto the destructor
> to
> > > deallocate X.
> > >
> > > Except that the keyword override next to my declared constructor and
> > > destructor causes an error message about overriding a static
> > method. So I
> > > leave out the override keyword and it compiles.
> > >
> > > But what is the difference?
> > >
> > > ============================================
> > > Patrick Dunford, Christchurch, NZ
> > > http://patrick.dunford.com/
> > >
> > > ------------------------------------------------------------------
> > > ---------
> > >     New Zealand Delphi Users group - Delphi List -
> [EMAIL PROTECTED]
> > >                   Website: http://www.delphi.org.nz
> > >
> >
> > ------------------------------------------------------------------
> > ---------
> >     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
> >                   Website: http://www.delphi.org.nz
> >
> 
> --------------------------------------------------------------------------
> -
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to