Binding a pointer temporary to a reference
<p>Please refer to the code below. In this code I am storing the <code>const char*</code> returned by <code>test.c_str()</code> into a reference. My question is - Will the <code>data</code> be correctly refering to the contents of <code>test</code>? I am thinking that the ptr returned by <code>test.c_str()</code> will be a temporary and if I bind it to a reference that reference will not be valid. </p>

<p>Is my thinking correct?</p>

<pre><code>class RefPtrTest
{
    std::string test;
    StoringClass storingClass;
public: 
    RefPtrTest(): test("hello"), storingClass(test.c_str())
    {
    }
}
</code></pre>

<p>where StoringClass is</p>

<pre><code>class StoringClass 
{
    const char*&amp; data;
public: 
    StoringClass (const char*&amp; input): data(input)
    {
    }
}
</code></pre>

<p><code>EDIT1:</code>
Let's just not consider what std::string is doing. Suppose I am using my own class called mystring</p>

<pre><code>class RefPtrTest
{
    const mystring test;
    StoringClass storingClass;
public: 
    RefPtrTest(): test("hello"), storingClass(test.getInternalPointer())
    {
    }
}
</code></pre>

<p><code>getInternalPointer</code> directly returns the internal pointer. I want to verify this hypothesis, while <code>storingClass(test.getInternalPointer())</code> ptr returned by <code>test.getInternalPointer()</code> will be a temporary and if I bind it to a reference that reference will not be valid. Is my thinking correct?</p>

<p><code>EDIT2:</code>
That StoringClass  is not under my control. Basically it's a template class where it stores reference to the type. I am using it for <code>const char*</code>. I am aware of all the design issues which you have raised. But I can't change that class, and I have to use it for <code>const char *</code>. there is no other way around it. </p>
