title: Default initialization of class data members in C++11
body: <p>I'm confused about the default initialization of the class data members. Here is the sample code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  class A {
  public:
    int i;
    A() {}
  };
  
  A a1;
  A aa1[3];
  std::vector&lt;A&gt; av1(3);
  
  int main()
  {
    A a2;
    A aa2[3];
    std::vector&lt;A&gt; av2(3);
    std::cout &lt;&lt; a1.i &lt;&lt; " " &lt;&lt; a2.i &lt;&lt; std::endl;          // 0         undefined
    std::cout &lt;&lt; aa1[0].i &lt;&lt; " " &lt;&lt; aa2[0].i &lt;&lt; std::endl;  // 0         undefined
    std::cout &lt;&lt; av1[0].i &lt;&lt; " " &lt;&lt; av2[0].i &lt;&lt; std::endl;  // undefined undefined
  }
  </code></pre>
  
  <p>In the code above, only <code>a1.i</code> and <code>aa1[0~2].i</code> are initialized to 0, while others are uninitialized. I don't know why is that happened.</p>
  
  <p>To be specifically, what I already know is that (From "C++ Primer"):</p>
  
  <ul>
  <li><p>The process of initialization is:</p>
  
  <ul>
  <li><code>a1</code> and <code>a2</code> are <em>default initialized</em>.</li>
  <li>every element of <code>aa1</code> and <code>aa2</code> are <em>default initialized</em>.</li>
  <li>every element of <code>av1</code> and <code>av2</code> are <em>value initialized</em>.</li>
  </ul></li>
  <li><p>The process of <em>default initialize</em> is:</p>
  
  <ul>
  <li>check if the variable is <em>built-in type</em> or <em>class type</em>.</li>
  <li>for <em>built-in type</em>, if the variable is outside any function body, then it is initialized to 0, else the value is undefined.</li>
  <li>for <em>class type</em>, if the class has default ctor, then that is called, else it is a compile error.</li>
  </ul></li>
  <li><p>The process of <em>value initialize</em> is:</p>
  
  <ul>
  <li>check if the variable is <em>built-in type</em> or <em>class type</em>.</li>
  <li>for <em>built-in type</em>, it is initialized to 0.</li>
  <li>for <em>class type</em>, it is <em>default initialized</em>. (Which I think means if the class has default ctor, then that is called, else it is a compile error.)</li>
  </ul></li>
  </ul>
  
  <p>So that when the ctor <code>A::A()</code> is called, how the data member <code>A::i</code> initialized (I guess it is default initialized)? And why only <code>a1.i</code> and <code>aa1[0~2].i</code> are initialized to 0, while others are uninitialized?</p>