[fpc-pascal] why constructor of TObject is NOT virtual but destructor is?

Sven Barth pascaldragon at googlemail.com
Mon May 5 08:07:44 CEST 2014


Am 05.05.2014 08:04 schrieb "Sven Barth" <pascaldragon at googlemail.com>:
>
> Am 05.05.2014 04:13 schrieb "Xiangrong Fang" <xrfang at gmail.com>:
> >
> > 2) What is the purpose of making destructor of TObject virtual?
>
> The destructor needs to virtual so that the correct one is called no
matter what static type a variable has. Imagine you have a TFoo which
inherits from TObject and you'd not have a virtual destructor, but only a
non-virtual one in both classes. Now if you call Free it would always be
TObject.Destroy that is called and not TFoo.Destroy, because Free is
declared in TObject and has no way if knowing that there is a TRio
descendant that has a constructor as well. And here virtual methods cone to
the rescue...

Of course that should have been TFoo at the end, not TRio... Stupid auto
completion -.-

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20140505/f181c171/attachment.html>


More information about the fpc-pascal mailing list