Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are very few ways a destructor gets called:</p> <p>1) You create an instance of <code>Drawable</code> on the stack, and it falls out of scope. If this is done in a tight loop, the object will fall out of scope and be destroyed at each iteration of the loop. For example:</p> <pre><code>for (size_t i = 0; i &lt; 100; ++i) { Drawable d; } </code></pre> <p>Here, 100 instances of <code>Drawable</code> will be created and destroyed, at the beginning and end of every loop.</p> <p>2) You <code>delete</code> a dynamically-allocated <code>Drawable</code>:</p> <pre><code>for (size_t i = 0; i &lt; 100; ++i) { Drawable* d = new Drawable; delete drawable; } </code></pre> <p>3) You call the destructor explicitly:</p> <pre><code>Drawable* b = new (buffer) Drawable; b-&gt;~Drawable() </code></pre> <p>Note that #3 uses "placement new" and is <em>highly</em> unlikely.</p> <hr> <p>Objects can be destroyed at suprising times when they are in a container such as a <code>vector</code>. Consider:</p> <pre><code>vector &lt;Drawable&gt; drawables; for (size_t i = 0; i &lt; 10000; ++i) { Drawable d; drawables.push_back (d); } </code></pre> <p>You will notice potentially many destructor calls when you run this code. When you <code>push_back</code>, a copy is potentially made and the original (<code>d</code> here) is destroyed. Also, when the <code>vector</code> reaches capacity it has to reallocate, which results in every item being copied again, and the originals destroyed.</p> <hr> <p>Objects can also be destroyed at suprising time in the face of temporaries and unexpected copies. Consider:</p> <pre><code>void DoSomething (Drawable d) { } int main() { Drawable d; for (size_t i = 0; i &lt; 1000; ++i) { DoSomething (d); } } </code></pre> <p>This is a naive example because the compiler will likely elide the temporaries in this case. But since <code>DoSomething()</code> takes a <code>Drawable</code> <em>by-value</em> a copy of the original could be made. Depending on other code, the compiler might not even be able to elide this copy.</p>
    singulars
    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.
 

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