title: Object creation and destruction order in C++
body: <p>I wrote a simple program to learn more about the order of creating and destructing objects in C++ (using Visual Studio 2015). Here it is:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class A
  {
  public:
      A(string name)
          : name(name)
      {
          cout &lt;&lt; "A(" &lt;&lt; name &lt;&lt; ")::constructor()" &lt;&lt; endl;
      }
      ~A()
      {
          cout &lt;&lt; "A(" &lt;&lt; name &lt;&lt; ")::destructor()" &lt;&lt; endl;
      }
  private:
      string name;
  };
  
  class C
  {
  public:
      C(string name, A a)
          : name(name), a(a)
      {
          cout &lt;&lt; "C(" &lt;&lt; name &lt;&lt; ")::constructor()" &lt;&lt; endl;
      }
      ~C()
      {
          cout &lt;&lt; "C(" &lt;&lt; name &lt;&lt; ")::destructor()" &lt;&lt; endl;
      }
  private:
      string name;
      A a;
  };
  
  class B
  {
  public:
      B(string name)
          : name(name)
      {
          cout &lt;&lt; "B(" &lt;&lt; name &lt;&lt; ")::constructor()" &lt;&lt; endl;
      }
      ~B()
      {
          cout &lt;&lt; "B(" &lt;&lt; name &lt;&lt; ")::destructor()" &lt;&lt; endl;
      }
  private:
      string name;
      A a1{"a1"};
      A a2{"a2"};
      C c1{"c1", a1};
      A a3{"a3"};
  };
  
  int main()
  {
      B b("b1");
      return 0;
  }
  </code></pre>
  
  <p>The output surprised me a little bit (the <code>a1</code>s):</p>
  
  <pre><code>A(a1)::constructor()
  A(a2)::constructor()
  C(c1)::constructor()
  A(a1)::destructor()
  A(a3)::constructor()
  B(b1)::constructor()
  B(b1)::destructor()
  A(a3)::destructor()
  C(c1)::destructor()
  A(a1)::destructor()
  A(a2)::destructor()
  A(a1)::destructor()
  </code></pre>
  
  <p>To learn more about what was going on I added information about the instances of objects:</p>
  
  <pre><code>    A(string name)
          : name(name)
      {
          cout &lt;&lt; "A(" &lt;&lt; name &lt;&lt; ")::constructor(), this = " &lt;&lt; this &lt;&lt; endl;
      }
      ~A()
      {
          cout &lt;&lt; "A(" &lt;&lt; name &lt;&lt; ")::destructor(), this = " &lt;&lt; this &lt;&lt; endl;
      }
  </code></pre>
  
  <p>The result was even more surprising:</p>
  
  <pre><code>A(a1)::constructor(), this = 0039FB28
  A(a2)::constructor(), this = 0039FB44
  C(c1)::constructor()
  A(a1)::destructor(), this = 0039F8A8
  A(a3)::constructor(), this = 0039FB98
  B(b1)::constructor()
  B(b1)::destructor()
  A(a3)::destructor(), this = 0039FB98
  C(c1)::destructor()
  A(a1)::destructor(), this = 0039FB7C
  A(a2)::destructor(), this = 0039FB44
  A(a1)::destructor(), this = 0039FB28
  </code></pre>
  
  <p>Namely, why is <code>a1</code>'s constructor only called <strong>once</strong> and destructor <strong>3 times</strong>? I'm passing <code>a</code> by value so obviously at least 1 temporary object is created but please explain to me <strong>when</strong> and <strong>how many</strong> <code>A</code> instances are created and destroyed?</p>