Hi there! I've created a octree class, which is going to be used in my 3d engine. Because I need to reuse it in various places, and the octree nodes can store different things, I've designed a generic class that implements every thing a octree need, addObject, deleteObject, etc.

Now, the problem is, when a octree needs to generate a new node, it calls a method CreateChild. The first version of this function was like this.

Code:
Function TOctreeNode.CreateChild(Box:TBoundingBox):TOctreeNode;
Begin
  Result:=TOctreeNode.Create(Box);
End;
However, since I want a generic octree, if I create another derived class, called TSceneOctreeNode, which I instantiated like this:
Octree:=TSceneOctreeNode.Create(WorldBBox);
You probably already have found the problem, when this octree needs to generate a new node, it creates a new TOctreeNode and not a TSceneTreeNode.
After investigating class fields I found something called ClassType and ClassName. ClassName holds the name of the class of the object, and I tough ClassType could be used to instantiate a new node.
So this is the new code:

Code:
Function TOctreeNode.CreateChild(Box:TBoundingBox):TOctreeNode;
Begin
  Result:=TOctreeNode(Self.ClassType).Create(Box);
End;
This should work in theory, but the program crashes after this, inside the constructor. Any expert can help me?