Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no guarantee that static members are destroyed only after all instances of an object of the same class. C++ incorporates no reference counting paradigm (<code>shared_ptr</code> notwithstanding).</p> <p>When considering lifetime, consider your static members as any other static object. There's really nothing binding them to their containing class other than being in the class's "namespace" (<em>warning: not accurate terminology</em>).</p> <p>So, if your <code>myClass</code> instances are created statically too, then you need to consider normal static object lifetime rules between them:</p> <p><a href="http://www.ideone.com/ig5rU" rel="nofollow">Example #1</a>:</p> <pre><code>#include &lt;iostream&gt; struct A { A() { std::cout &lt;&lt; "*A "; }; ~A() { std::cout &lt;&lt; "~A "; }; }; struct B { B() { std::cout &lt;&lt; "*B "; }; ~B() { std::cout &lt;&lt; "~B "; }; static A a; }; B t; A B::a; int main() {} // Output: *B *A ~A ~B </code></pre> <p>As you can see, the static member of <code>B</code> is destroyed before the actual instance of <code>B</code>.</p> <p><a href="http://www.ideone.com/mhIO1" rel="nofollow">Example #2:</a></p> <pre><code>#include &lt;iostream&gt; struct A { A() { std::cout &lt;&lt; "*A "; }; ~A() { std::cout &lt;&lt; "~A "; }; }; struct B { B() { std::cout &lt;&lt; "*B "; }; ~B() { std::cout &lt;&lt; "~B "; }; static A a; }; A B::a; B t; int main() {} // Output: *A *B ~B ~A </code></pre> <p>Here the reverse is true. It's a cheap fix for your current issue, but my advice is to avoid static instances altogether; you'll only fall into more static initialisation pitfalls like this down the line... possibly with a future incarnation of the same code!</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. 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.
 

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