C++ const reference member extending lifetime of object
<p>This is related to <a href="http://stackoverflow.com/questions/6936720/why-lifetime-of-temporary-doesnt-extend-till-lifetime-of-enclosing-object">a question posted yesterday</a>. </p>

<pre><code>class A
{
public:
    mutable int x;
   A()  
   { 
      static int i = 0; 
      x = i; 
      i++;  
      std::cout &lt;&lt; " A()" &lt;&lt; std::endl; 
   }
   ~A() 
   { 
       std::cout &lt;&lt; "~A()" &lt;&lt; std::endl; 
   }
   void foo() const 
   { 
       x = 1; 
   };
};

class B
{
public:
   const A &amp; a;
   B(const A &amp; a) : a(a) 
   { 
       std::cout &lt;&lt; " B()" &lt;&lt; std::endl; 
   }
   ~B() 
   { 
       std::cout &lt;&lt; "~B()" &lt;&lt; std::endl; 
   }
   void doSomething() 
   { 
       a.foo(); 
   };
};

int main() 
{
   B b((A()));
   b.doSomething();
}
</code></pre>

<p>Now, a's destructor is called before the call to doSomething. However, the call works although the function basically changes a member of A. Is it not the same instance. No other A's are created. I used the static inside A's constructor to keep track of that. Can anyone explain?</p>
