Returning a smart pointer and assign it a value doesn't work
<p>I am having some problems modifying the content of a smart pointer returned by a class function. Returning a reference to the pointer would be a solution, but I am concerned of it as a bad practice.</p>

<p>This is my code:</p>

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

class Foo
{
public:
    Foo() : ptr_(new int(5)) {};
    ~Foo() {};

    std::shared_ptr&lt;int&gt; bar()
    {
        return ptr_;
    }

    void print()
    {
        std::cout &lt;&lt; *ptr_ &lt;&lt; std::endl;
    }

private:
    std::shared_ptr&lt;int&gt; ptr_;
};

int main()
{
    Foo f;
    f.print();

    // First case
    f.bar() = std::make_shared&lt;int&gt;(23);
    f.print();

    // Second case
    f.bar().reset(new int(23));
    f.print();

    // Third case
    *f.bar() = 23;
    f.print();

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

<p>And this is the output:</p>

<pre><code>5
5
5
23
</code></pre>

<p>Why only in the third case ptr_ changes its value?</p>
