I am writing a quadtree class as a part of graphics library and I am facing a design issue. A main goal is to allow the user of the library to easily extend the quadtree with their own Node Types. Each node has a pointer to the first of its four childs. I use the protoype pattern to 'clone' a parent node (its real type is unknown to the library) four times when it is split. So here is the Node class:
class CNode {
public:
virtual CNode* clone();
protected:
CNode* pChilds;
}
The user of the library may now define its own node and add a traverse method:
class MyNode : public CNode {
public:
virtual CNode* clone() {
return new MyNode;
}
void myTraverse() {
if(pChilds[0] != nullptr)
static_cast<MyNode*>(pChilds[0])->traverse();
}
}
As can be seen I have to do a cast from the base class to the derived class. Alternatively I could make all quadtree related classes templates but I really don't want to do that. I also cant use use boost. Besides that boost::any and similar solutions with RTTI or dynamic casting are to slow since the quadtree is a performance critical component and must to run as fast as possible!
Is there any possebility to maintain the speed of the static_cast while adding some type safety? (a quadtree will only contain nodes of a single type).