Passing shared_ptr of this in constructor
<p>I have a code like the following</p>

<pre class="lang-cpp prettyprint-override"><code>class Task;

class Attribute
{
public:
    Attribute(Task* task, std::string name);

    std::shared_ptr&lt;Task&gt; task_;
    std::string name_;
};

class Task : std::enable_shared_from_this&lt;Task&gt;
{
public:
    std::shared_ptr&lt;Task&gt; getSharedPtr()
    {
        return this-&gt;shared_from_this();
    }

    Attribute att1_ = {this, "attribute1"};
    Attribute att2_ = {this, "attribute2"};
};

Attribute::Attribute(Task* task, std::string name)
    : name_(name)
{
   task_ = task-&gt;getSharedPtr();
}
</code></pre>

<p>When I run it I get a </p>

<pre><code>$ terminate called after throwing an instance of 'std::bad_weak_ptr'
</code></pre>

<p>from this <a href="https://stackoverflow.com/questions/5558734/c-bad-weak-ptr-error">question</a> I got that I cannot call <code>shared_from_this()</code> untill the object is fully constructed.
I guess this means that I cannot create objects of type <code>Attribute</code> in the constructor, but I should instantiate them after the constructor ends.</p>

<p>Do you know if it is a way to overcome this problem, because it would be really annoying for me to have to instantiate the Attribute objects not in the constructor.</p>