Use a std::vector<Node*>
:
Node::Node( const string &p_data, const int &p_levels ):
_data(p_data),
_nextNode(p_levels)
{
this will initialise the elements of _nextNode
to nullptr
s.
Consider using a smart pointer implementation instead of Node*
to manage the destruction of the Node
instances for you.
If you have to use a Node*
then you need a Node**
to point to a list of Node*
:
class Node
{
private:
Node**_nextNode;
string _data;
};
Node::Node( const string &p_data, const int &p_levels ):
_data(p_data),
_nextNode(new Node*[p_levels]())
{ //^^ value initialization.
allocates an array of int*
containing p_levels
elements and value initializes them (sets them to NULL
). Node
needs to know how many elements are stored in _nextNode
so p_levels
would require storing also. Destruction:
for (int i = 0; i < _nextNodeElements; i++)
{
delete _nextNode[i]; // delete NULL is safe, a no-op.
}
delete[] _nextNode;
Just to push you toward std::vector
again: std::vector<std::unique_ptr<Node>> _nextNode;
wouldn't require a hand-written destructor, the default generated one would suffice.