Why dereferencing a unique_ptr won't modify the original object
<p>See the following example, I've used a unique pointer and a raw pointer to <code>a</code>, my question is, why does the raw pointer work but not the unique pointer? If I want to modify string <code>a</code> like a reference by using the <code>unique_ptr</code> or <code>shared_ptr</code>, what should I do? </p>

<p>Example program:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;memory&gt;

int main()
{
    using namespace std;
    string a = "aaa";
    auto ptr = std::make_unique&lt;string&gt;(a);
    auto ptr2 = &amp;a;
    cout &lt;&lt; "before, a: " &lt;&lt; a &lt;&lt; endl;
    *ptr += "bbb";
    cout &lt;&lt; "After, a: " &lt;&lt; a &lt;&lt; endl;
    *ptr2 += "ccc";
    cout &lt;&lt; "after 2, a: " &lt;&lt; a &lt;&lt; endl;
}
</code></pre>

<p>Output:</p>



<pre><code>before, a: aaa
After, a: aaa
after 2, a: aaaccc
</code></pre>