safe deletion of pointer, trees

  • Thread starter Christopher Pisz
  • Start date
C

Christopher Pisz

currently my node is like so with everything public:

class Quadtree_Node
{
public:
Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds);
~Quadtree_Node();
void Subdivide(float smallest_node);
const Quadtree_Node *& GetTopLeft();
Bounding_Box m_bounds; // Bounding Coordinates of this node
Quadtree_Node * m_parent; // Parent of this node, NULL if root
Quadtree_Node * m_tlchild; // Top Left child node
Quadtree_Node * m_trchild; // Top Right child node
Quadtree_Node * m_blchild; // Bottom Left child node
Quadtree_Node * m_brchild; // Bottom Right child node
std::list<Data> m_data; // Linked List of Data
};

And my deletion method looks like:

void Quadtree::Delete(Quadtree_Node * node)
{
if(node)
{
Delete(node->m_tlchild);
Delete(node->m_trchild);
Delete(node->m_blchild);
Delete(node->m_brchild);
}
delete node;
}

How can I change this so that I can make my data members of class
Quadtree_Node private? I was thinking of making a GetFunction to get the
pointers to the children of the node, but then I am not sure if I would be
returning a copy of the pointer, a referance of the pointer or the pointer
itself for use in the deletion.

A try:

const Quadtree_Node *& GetTopLeft() {return m_tlchild;}
delete ->node->GetTopLeft();

Would that be safe?

Thanks,
Christopher
 
H

Howard

Christopher Pisz said:
currently my node is like so with everything public:

class Quadtree_Node
{
public:
Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds);
~Quadtree_Node();
void Subdivide(float smallest_node);
const Quadtree_Node *& GetTopLeft();
Bounding_Box m_bounds; // Bounding Coordinates of this node
Quadtree_Node * m_parent; // Parent of this node, NULL if root
Quadtree_Node * m_tlchild; // Top Left child node
Quadtree_Node * m_trchild; // Top Right child node
Quadtree_Node * m_blchild; // Bottom Left child node
Quadtree_Node * m_brchild; // Bottom Right child node
std::list<Data> m_data; // Linked List of Data
};

And my deletion method looks like:

void Quadtree::Delete(Quadtree_Node * node)
{
if(node)
{
Delete(node->m_tlchild);
Delete(node->m_trchild);
Delete(node->m_blchild);
Delete(node->m_brchild);
}
delete node;
}

You should, in general, allow the object being deleted to handle deleting
its own members. So, you should simply call delete on node itself, and
allow the destructor for the Quadtree_Node to delete all its members. Then,
the members can be public, protected, or private, as you desire.

Unless there is more code you haven't shown in the Delete function, you can
simply remove that, and call delete directly on the node you were previously
passing to Quadtree::Delete. The Quadtree::Delete function, as it is shown
above, is simply not needed.

You'll also probably want an assignment operator for your Quadtree_Node
class. (Google for the "rule of three" to see why.)

-Howard
 
J

John Harrison

Christopher Pisz said:
currently my node is like so with everything public:

class Quadtree_Node
{
public:
Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds);
~Quadtree_Node();
void Subdivide(float smallest_node);
const Quadtree_Node *& GetTopLeft();
Bounding_Box m_bounds; // Bounding Coordinates of this node
Quadtree_Node * m_parent; // Parent of this node, NULL if root
Quadtree_Node * m_tlchild; // Top Left child node
Quadtree_Node * m_trchild; // Top Right child node
Quadtree_Node * m_blchild; // Bottom Left child node
Quadtree_Node * m_brchild; // Bottom Right child node
std::list<Data> m_data; // Linked List of Data
};

And my deletion method looks like:

void Quadtree::Delete(Quadtree_Node * node)
{
if(node)
{
Delete(node->m_tlchild);
Delete(node->m_trchild);
Delete(node->m_blchild);
Delete(node->m_brchild);
}
delete node;
}

How can I change this so that I can make my data members of class
Quadtree_Node private? I was thinking of making a GetFunction to get the
pointers to the children of the node, but then I am not sure if I would be
returning a copy of the pointer, a referance of the pointer or the pointer
itself for use in the deletion.

There is no reason in the above code that you can't just make them private
as it is. I mean what's the problem, Delete is a member of Quadtree so it is
allowed to access private members of Quadtree, even if those are members of
a different node. Try it and see.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top