Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off, if you make the base class destructor <code>virtual</code>, all derived classes will automatically get a <code>virtual</code> destructor if you declare them as <code>virtual</code> or not. This is generally true for matching signatures: if a base class has a <code>virtual</code> function with the same signature as a function in a derived class, the function in the derived class is an <code>override</code> and is <code>virtual</code> (although in C++ 2011 you can prevent further overriding using the <code>final</code> keyword in which case another override would create an error).</p> <p>That said, destructors are special: when you make a destructor <code>virtual</code> it will still be called even if there is another overriding destructor! The only impact of a destructor being <code>virtual</code> is what happens if you <code>delete</code> an object using a pointer to a base class when the object actually happens to be of a derived type: If the destructor isn't <code>virtual</code> you get undefined behavior while the Right Thing happens if the destructor is <code>virtual</code>. For example:</p> <pre><code>class not_a_base {}; class bad_idea: public not_a_base {}; class a_base { public: virtual ~a_base() {} }; class ok: public a_base {}; int main() { a_base* ab = new ok; delete ab; // &lt;---- all is good here! not_a_base* nab = new bad_idea; delete nab; // &lt;---- results in undefined behavior } </code></pre> <p>The reason destructors are not <code>virtual</code> by default is simply that this would mean that object size is always increased by a word size which is unacceptable in general.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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