Basically I am creating a base class that will be used for classes stored as a linked list, which are traversed and deleted as dictated by a virtual update() function which returns a bool.
I am wondering if this is the most efficient case (I like the fact that it can be a singley linked list in particular):
class Traversable
{
public:
Traversable();
virtual ~Traversable();
void traverse(Traversable** prevNext);
virtual bool update()=0;
protected:
private:
Traversable* next;
};
void Traversable::traverse(Traversable** prevNext)
{
if (!update()) /// Virtual function that returns a death flag
{ /// Death
if (next)
{
Traversable* localNext = next;
delete this;
localNext->traverse(prevNext);
}
else
{
*prevNext = NULL;
delete this;
}
}
else
{ /// This node stays alive, for now
*prevNext = this;
if (next)
{
next->traverse(&next);
}
}
}
Note the linked list is NULL terminated.
I think the careful lack of an assigment operation to a local variable after the next traverse function is called will secure usage of this function using tail calls. Can anyone spot anything I have done wrong, or perhaps suggest a slightly less convoluted approach :p