-1

I've tried two different things, but I'm not entirely sure what is happening. Looking for an explanation and input on how to do this in the best possible way, thanks! I don't want to use a vector.

Graph.h

public:
   void setNodeList(int num);
private:
   Node *nodeList[];

Graph.cpp

void Graph::setNodeList(int num)
{
   nodeList[num]; // Would this work?
   *nodeList = new Node[num]; // Or this? Not really sure.
}

1 Answers1

0

If you want that your array is situated on the heap ("permanently" stored, and removed after the methods returns), then you should use

*nodeList = new Node[num];

Be aware to delete your allocated memory via delete[] nodeList and NOT via delete nodeList (without []). This stackoverflow answer explains why.

As @StoryTeller suggests I would also check the STL std::vector, which represents a dynamic collection.

Moerwald
  • 10,448
  • 9
  • 43
  • 83