Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Modified you program :</p> <pre><code>#include &lt;iostream&gt; using namespace std; class Foo { public: Foo () { c = 'a'; cout &lt;&lt; "Foo()" &lt;&lt; endl; } Foo (char ch) { c = ch; cout &lt;&lt; "Foo(char)" &lt;&lt; ch &lt;&lt; endl; } ~Foo () { cout &lt;&lt; "~Foo()"&lt;&lt; c &lt;&lt; endl; } protected: char c; }; class Bar : public Foo { public: Bar () { cout &lt;&lt; "Bar()" &lt;&lt; endl; } Bar (char ch) : Foo(ch) { cout &lt;&lt; "Bar(char)" &lt;&lt; ch &lt;&lt; endl; } ~Bar () { cout &lt;&lt; "~Bar()" &lt;&lt; c &lt;&lt; endl; } }; Foo f1('a'); static Bar b1('b'); int main() { Bar b2('c'); { static Foo f2('d'); Foo f3('e'); Bar b3 ('f'); } return 0; } </code></pre> <p>Which generates the following output in g++ 4.5.2:</p> <pre><code>Foo(char)a Foo(char)b Bar(char)b Foo(char)c Bar(char)c Foo(char)d Foo(char)e Foo(char)f Bar(char)f ~Bar()f ~Foo()f ~Foo()e ~Bar()c ~Foo()c ~Foo()d ~Bar()b ~Foo()b ~Foo()a </code></pre> <p>You see that the last destructed one is the non-static global variable <code>Foo f1</code>.</p> <p>EDIT: As the others mentioned, the initialization order of variables with static storage duration is unspecific if the variables are from different translation units, but they can be defined when they are in the same translation unit.</p> <p>Initialization by constructor calls (as in this examples) are called <code>dynamic initialization</code>, and </p> <blockquote> <p><strong>Dynamic initialization of a non-local variable with static storage duration is either ordered or unordered</strong>. Definitions of explicitly specialized class template static data members have ordered initialization. Other class template static data members (i.e., implicitly or explicitly instantiated specializations) have unordered initialization. <strong>Other non-local variables with static storage duration have ordered initialization. Variables with ordered initialization defined within a single translation unit shall be initialized in the order of their definitions in the translation unit.</strong></p> <p>It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first odr-use (3.2) of any function or variable defined in the same translation unit as the variable to be initialized.</p> </blockquote> <p>The initialization of local static variables is specified as</p> <blockquote> <p>... such a variable is initialized the first time control passes through its declaration; ...</p> </blockquote> <p>And as the destruction of variables with static storage duration should be in the reverse order of their construction, so the order of construction and destruction of the variables with types <code>Foo</code> and <code>Bar</code> in this example is in fact defined.</p> <p>Again, when you have multiple translation, you'd better not to rely on the order of initialization.</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.
    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