Returning constructor-less object as output parameter
<p>I want to return an object from a function as an output parameter, but the object has no default constructor so I can't do this:</p>

<pre><code>bool FindFlaggedObject(MyObject&amp; myObject)
{
    std::vector&lt;MyObject&gt; myObjects = GetSomeObjectList();
    for (UINT i = 0; i &lt; myObjects.size(); i++)
    {
        if (myObjects[i].Flag) {
            myObject = myObjects[i];
            return true;
        }
    }
    return false;
}

void main()
{
    MyObject myObject;  // NOT ALLOWED - OBJECT HAS NO DEFAULT CONSTRUCTOR
    if (FindFlaggedObject(myObject))
    {
        ...
    }
}
</code></pre>

<p>So, it looks like I should return it on the heap and manage it with a shared_ptr instance, like this:</p>

<pre><code>bool FindFlaggedObject(MyObject&amp; myObject)
{
    std::vector&lt;MyObject&gt; myObjects = GetSomeObjectList();
    for (UINT i = 0; i &lt; myObjects.size(); i++)
    {
        if (myObjects[i].Flag) {
            myObject = new MyObject(myObjects[i]);
            return true;
        }
    }
    return false;
}

void main()
{
    MyObject* myObjectPtr;
    if (FindFlaggedObject(myObjectPtr))
    {
        std::shared_ptr&lt;MyObject&gt; myObject(myObjectPtr);

        ...
    }
}
</code></pre>

<p>The downside is that anyone calling the method will have to remember that he is responsible for deallocating the object.<br>
<strong>What is the best practice for returning constructor-less objects as output parameters?</strong></p>
