J
Jason
Hi,
I have a class representing a tree-like structure called Node. Each Node
contains a vector of pointers to other nodes ie stl::vector<Node*>. The
Node class itself contains a destructor method to delete all the nodes
within its vector as below:
Node::~Node() {
for(vector<Node*>::iterator it = outedges.begin(); it != outedges.end();
++it)
delete *it;
}
All the nodes were taken from dynamic memory using 'Node* newnode = new
Node; node->add(newnode);' etc, except the initial Root node, which is just
an attribute of another class. My question is, if I call the destructor
method on just the root node, what will happen to all the nodes pointed to
by its sub-nodes amd the sub-nodes sub-nodes etc. Will the entire tree be
deleted or will it leave pointers with no owner, meaning that I should
traverse the entire tree, deleting all leaf nodes, then moving back up to
finally delete the root? I'm hoping that I can just destroy the root and
then everything else will be ok.
Thanks for your help
Jason.
I have a class representing a tree-like structure called Node. Each Node
contains a vector of pointers to other nodes ie stl::vector<Node*>. The
Node class itself contains a destructor method to delete all the nodes
within its vector as below:
Node::~Node() {
for(vector<Node*>::iterator it = outedges.begin(); it != outedges.end();
++it)
delete *it;
}
All the nodes were taken from dynamic memory using 'Node* newnode = new
Node; node->add(newnode);' etc, except the initial Root node, which is just
an attribute of another class. My question is, if I call the destructor
method on just the root node, what will happen to all the nodes pointed to
by its sub-nodes amd the sub-nodes sub-nodes etc. Will the entire tree be
deleted or will it leave pointers with no owner, meaning that I should
traverse the entire tree, deleting all leaf nodes, then moving back up to
finally delete the root? I'm hoping that I can just destroy the root and
then everything else will be ok.
Thanks for your help
Jason.