The constness of a variable and its lifetime
<p>So from <a href="http://stackoverflow.com/questions/3097593/what-happens-when-c-reference-leaves-its-scope">a question</a> asked in another thread, I have thought of a new question and the answer is not obvious to me. </p>

<p>So it appears there is a c++ rule that says if you have a const reference to a temporary, then the lifetime of the temporary is at least as long as the const reference. But what if you have a local const reference to another object's member variable and then when you leave scope - Does it call the destructor of that variable?</p>

<p>So here is modified program from the original question:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

class A {
public:
   A(std::string l) { k = l; };
   std::string get() const { return k; };
   std::string k;
};

class B {
public:
   B(A a) : a(a) {}
   void b() { cout &lt;&lt; a.get(); }  //Has a member function
   A a;
};

void f(const A&amp; a)
{  //Gets a reference to the member function creates  a const reference
     stores it and goes out of scope
 const A&amp; temp = a;
 cout &lt;&lt; "Within f(): " &lt;&lt; temp.k &lt;&lt; "\n";
}

int main() {
   B b(A("hey"));

   cout &lt;&lt; "Before f(): " &lt;&lt; b.a&lt;&lt; "\n";

   f(b.a);

   cout &lt;&lt; "After f(): " &lt;&lt; b.a.k &lt;&lt; "\n";

   return 0;
}
</code></pre>

<p>So when I run this code, I get "hey" as the value everytime. Which seems to imply that a local const reference does not bind itself through life with a passed in member object. Why doesn't it? </p>
