Xinus
Xinus

Reputation: 30563

Is there any relation between Virtual destructor and Vtable

If we write virtual function it adds a vtable in object of that class. Is it true for virtual destructor too ? Is vtable used to implement virtualness of destructor

Upvotes: 1

Views: 2129

Answers (6)

Naveen
Naveen

Reputation: 73503

It is treated like any other normal function and will be added to the vtable.

Upvotes: 0

Jonathan Graehl
Jonathan Graehl

Reputation: 9301

Yes. Some information is needed to allow the right destructor to be called when the object is deleted via a base class pointer. Whether that information is a small integer index or a pointer doesn't matter (although dynamic linkage probably implies that it's a pointer). Naturally, that information needs to be adjacent to (inside) the pointed-to object.

Adding a virtual method of any kind, including a destructor, to a class that had none before, will increase sizeof(class).

Upvotes: 5

aJ.
aJ.

Reputation: 35490

Yes. Virtual destructor is like any other virtual method. Vtable entry will get added.

Upvotes: 2

Peter Cardona
Peter Cardona

Reputation: 2110

I don't believe that the C++ standard requires any particular mechanism for producing the correct behavior, but yes, that's a typical implementation. A class with at least 1 virtual function has a table of (virtual) function pointers, the destructor being one of them, if it's marked virtual.

Upvotes: 4

Mark Ransom
Mark Ransom

Reputation: 308520

Yes it is. Sorry I don't have a definitive reference to back up my assertion. But how else would you get different behavior when using just a pointer to the object?

Upvotes: 2

Related Questions