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

2014-05-05 Thread Marco van de Voort
In our previous episode, Xiangrong Fang said:
> I would like to know the benefit of making constructor / destructor
> virtual.  As described here:
> 
> http://c2.com/cgi/wiki?VirtualConstructor
> 
> The benefit of virtual constructor seems related to "class reference". As
> far as I understand, it is related to how LCL works, like:
> 
> CreateComponent(AComponent: TComponentClass);
> 
> My questions are:
> 
> 1) if the above understanding is correct AND COMPLETE, why the constructor
> of TObject is not virtual? What problem will appear IF we make it virtual?

> 2) What is the purpose of making destructor of TObject virtual?

I think you are thinking in the right direction, parameters is the reason.
This means that a framework that wants two or differently typed parameters
to the constructor can introduce its own deepest level constructor.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


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

2014-05-04 Thread Sven Barth
Am 05.05.2014 08:04 schrieb "Sven Barth" :
>
> Am 05.05.2014 04:13 schrieb "Xiangrong Fang" :
> >
> > 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
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

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

2014-05-04 Thread Sven Barth
Am 05.05.2014 04:13 schrieb "Xiangrong Fang" :
> My questions are:
>
> 1) if the above understanding is correct AND COMPLETE, why the
constructor of TObject is not virtual? What problem will appear IF we make
it virtual?

Virtual constructors are basically only needed if you want to create an
object using a class reference variable. So there's no need to burden every
constructor in the (Object Pascal) world with the overhead of a virtual
method call...

>
> 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...

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

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

2014-05-04 Thread Xiangrong Fang
Hi All,

I would like to know the benefit of making constructor / destructor
virtual.  As described here:

http://c2.com/cgi/wiki?VirtualConstructor

The benefit of virtual constructor seems related to "class reference". As
far as I understand, it is related to how LCL works, like:

CreateComponent(AComponent: TComponentClass);

My questions are:

1) if the above understanding is correct AND COMPLETE, why the constructor
of TObject is not virtual? What problem will appear IF we make it virtual?

2) What is the purpose of making destructor of TObject virtual?

Thanks!

Xiangrong
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal