In my implementation, I have a vector of classes. Within each class there is a unique_ptr
to a linked list. Only at runtime do I know the number of nodes that should be added to each of the linked lists. Some linked lists may have zero nodes.
A simplified view of my class is:
class A
{
private:
...
std::unique_ptr< std::list<MyListElement> > ptrList;
...
public:
...
};
Thanks to the unique_ptr
, I had to jump through the hoops of first declaring a copy-constructor and copy-assignment-operator and setting them to = delete
, and then providing definitions for a default-constructor, move-constructor and move-assignment-operator. After all this, I am now ready to call the function that initializes my linked lists for each object.
void A::initListElements(unsigned int numElements)
{
if (numElements > 0)
{
std::unique_ptr< std::list<MyListElement> > tmp(new std::list<MyListElement>);
ptrList = std::move(tmp);
}
else
{
ptrList = 0;
}
}
Is this the correct way of doing it? Is there some way I can avoid creating the temporary unique_ptr 'tmp'?