Original Post
The other day I wrote a quadtree implementation, and instead of writing a DeleteTree() function that recurses, I just added a recursive destructor. I did it in about a minute and didn't really think about it...so I guess I'm just a little paranoid about whether it's actually 'right'.
struct QuadNode
{
QuadNode* Children[4];
int NumPatches;
int* Patches;
Vector Min, Max, Pos;
QuadNode() : NumPatches( 0 ), Patches( NULL )
{
Children[0] = NULL;
Children[1] = NULL;
Children[2] = NULL;
Children[3] = NULL;
}
~QuadNode()
{
if( Patches != NULL )
{
delete[] Patches;
Patches = NULL;
}
NumPatches = 0;
for( int i = 0; i < 4; ++i )
if( Children[i] )
{
delete Children[i];
Children[i] = NULL;
}
}
};
Then when the parent class goes to delete the tree, it simply says delete m_RootNode; and lets the destructor do the rest. I think it's right, but like I said, I'm paranoid, and also a little tired.
Does it do what it's supposed to?
[edited by - Promit on June 12, 2004 10:49:30 AM]