Reputation: 30563
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
Reputation: 94653
Take a look at http://geneura.ugr.es/~jmerelo/c++-faq/virtual-functions.html#faq-20.5
Upvotes: 0
Reputation: 73503
It is treated like any other normal function and will be added to the vtable.
Upvotes: 0
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
Reputation: 35490
Yes. Virtual destructor is like any other virtual method. Vtable entry will get added.
Upvotes: 2
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
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