title: Initializing Unique Pointers as a Class Member
body: <p>I am working on a tree structure that has a vector of <code>unique pointers</code> as a class member. While working on it, I realised that I am not sure how to initialize a unique pointer as a class member if the object, which the unique pointer will own, comes as a parameter.</p>
  
  <p>So, I am simply asking: How I should initialize unique pointers as a class member if the object that will be owned comes as a parameter?</p>
  
  <p>Should I use new ( <code>addTheObject(new Object())</code> ) and then use <code>std::make_unique</code>? Should I pass the object as unique_ptr ( <code>addTheObject(std::unique_ptr&lt;Object&gt;&amp; theObject)</code> ) and use <code>std::move(theObject)</code>?</p>
  
  <p>What is the correct way to handle this?</p>
  
  <p>In case you need more concrete example:</p>
  
  <p>I have a <code>DT_Node</code> class, which is a node of a tree, and I will construct a tree using <code>DT_Node</code>.</p>
  
  <p><code>DT_Node</code> has a method called <code>addChild()</code> that is used to insert children into its node vector.</p>
  
  <p><code>DT_Node</code> will be used in different cpp files to construct a tree, which means another cpp file will use <code>addChild()</code> method to add the children of the node.</p>
  
  <pre><code>// DT_Node.hpp
  class DT_Node
  {
  public:
  
      DT_Node();
      virtual ~DT_Node();
  
      virtual void decide() = 0;
  
      virtual void addChild( ??? );
  
  private:
  
      std::vector&lt; std::unique_ptr&lt;DT_Node&gt; &gt; mNodes;
  };
  </code></pre>
  
  <hr>
  
  <pre><code>// DT_Node.cpp
  DT_Node::DT_Node()
  {
  
  }
  
  DT_Node::~DT_Node()
  {
      mNodes.clear();
  }
  
  void DT_Node::addChild( ??? )
  {
      ???
  }
  </code></pre>