Note that there are some explanatory texts on larger screens.

plurals
  1. POShould every class have a virtual destructor?
    primarykey
    data
    text
    <p>Java and C# support the notion of classes that can't be used as base classes with the <code>final</code> and <code>sealed</code> keywords. In C++ however there is no good way to prevent a class from being derived from which leaves the class's author with a dilemma, should every class have a virtual destructor or not?</p> <hr> <p><strong>Edit:</strong> Since C++11 this is no longer true, you can specify that a class is <a href="http://en.cppreference.com/w/cpp/language/final" rel="noreferrer"><code>final</code></a>.</p> <hr> <p>On the one hand giving an object a virtual destructor means it will have a <code>vtable</code> and therefore consume 4 (or 8 on 64 bit machines) additional bytes per-object for the <code>vptr</code>.</p> <p>On the other hand if someone later derives from this class and deletes a derived class via a pointer to the base class the program will be ill-defined (due to the absence of a virtual destructor), and frankly optimizing for a pointer per object is ridiculous. </p> <p>On the <a href="http://en.wikipedia.org/wiki/Gripping_hand" rel="noreferrer">gripping hand </a> having a virtual destructor (arguably) advertises that this type is meant to be used polymorphically.</p> <p>Some people think you need an explicit reason to not use a virtual destructor (as is the subtext of <a href="https://stackoverflow.com/questions/300986">this question</a>) and others say that you should use them only when you have reason to believe that your class is to be derived from, what do <em>you</em> think?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload