views:

306

answers:

6

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

+2  A: 

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?

Mark Ransom
There are other mechanisms to implement virtual methods. vtables are just the most common (and easy to implement).
Martin York
+4  A: 

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.

+2  A: 

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

aJ
+4  A: 

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

wrang-wrang
A: 

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

Naveen