Ask question, find answer on any topic in real time from people around the world. Have a question ? Ask now. Know an Answer share your Knowledge to world.PrepJunk is the online community that has Junk of answers!
UNITED STATES
2
2
online

What is a ‘virtual destructor’?

0

What is a ‘virtual destructor’?

asked Apr 7, 2012 in C++ by mohan (541 points)
    

1 Answer

0

In general, a virtual fn means to start at the class of the object itself, not the type of the pointer/ref (‘do the right thing based on the actual class of’ is a good way to remember it). Virtual destructors (dtors) are no different: start the destruction process ‘down’ at the object’s actual class, rather than ‘up’ at the ptr’s class (ie: ‘destroy yourself using the correct destruction routine’).

Virtual destructors are so valuable that some people want compilers to holler at you if you forget them. In general there’s only one reason not to make a class’ dtor virtual: if that class has no virtual fns, the introduction of the first virtual fn imposes typically 4 bytes overhead in the size of each object (there’s a bit of magic for how C++ ‘does the right thing’, and it boils down to an extra ptr per object called the ‘virtual table pointer’ or ‘vptr’).

answered Apr 7, 2012 by mohan (541 points)