Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-05

why constructor of TObject is NOT virtual but destructor is?

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

Sven Barth
to Xiangrong Fang
"Xiangrong Fang" wrote
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

Reply by email

Sven Barth
to Sven Barth
"Sven Barth" wrote 6 lines
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

Reply by email

Marco van de Voort
to Xiangrong Fang: I would like to know the benefit of making constructor / destructor virtual. As…

In our previous episode, Xiangrong Fang said:

Show quoted text 16 lines
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.

Reply by email