mardi 3 février 2015

Tree of template class where chilldren are std::array of std::unique_ptr

I am in the following situation:



template<size_t Level>
class Node
{
public:

Node()
{
for (size_t i = 0; i < children.size(); i++)
children[i].reset(new Node<Level - 1>());
}

array<unique_ptr<Node<Level - 1>>, 4> children;
};


template<>
class Node<0>
{
};

...

Node<2> root;


I need to create a tree of nodes, where each node, but the last (level = 0), has 4 children. I thought to store the children in a std:array of std::unique_ptr.


Is my solution right or am I doing somenthing wrong and there is a smarter way to achieve this result?


Aucun commentaire:

Enregistrer un commentaire