I am working on a tree structure that has a vector of unique pointers 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.
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?
Should I use new ( addTheObject(new Object()) ) and then use std::make_unique? Should I pass the object as unique_ptr ( addTheObject(std::unique_ptr<Object>& theObject) ) and use std::move(theObject)?
What is the correct way to handle this?
In case you need more concrete example:
I have a DT_Node class, which is a node of a tree, and I will construct a tree using DT_Node.
DT_Node has a method called addChild() that is used to insert children into its node vector.
DT_Node will be used in different cpp files to construct a tree, which means another cpp file will use addChild() method to add the children of the node.
// DT_Node.hpp
class DT_Node
{
public:
DT_Node();
virtual ~DT_Node();
virtual void decide() = 0;
virtual void addChild( ??? );
private:
std::vector< std::unique_ptr<DT_Node> > mNodes;
};
// DT_Node.cpp
DT_Node::DT_Node()
{
}
DT_Node::~DT_Node()
{
mNodes.clear();
}
void DT_Node::addChild( ??? )
{
???
}
What you need is:
void DT::Node::addChild(std::unique_ptr<DT_Node>&& child) {
mNodes.emplace_back(std::move(child));
}
By the way, your title is misleading. The unique_ptr is not a member (it is contained in a member), and you are not definitely not initializing a member (for which you would use the member initializer list syntax in the constructor).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With