Say, generic class X has the attribute A, that attribute A can be either an object of generic class X (that we don't know its attribute A type yet) or a string, depending on certain conditions that are evaluated at runtime.
In my quest for implementing a quad tree, I've reached the point where I can generate it but I now want to store something on it, thus the need of generics. Ideally, in my quad tree, only the leaves will store something, the rest of the nodes won't.
I've implemented it in a way I'm only using one class "QuadTree" for the whole thing. The simple solution is just add an attribute "storedThing" of type objT, initialize it with "null" if its a root or mid child node, if its a leaf I assign to it the object I'm storing and be done with it. But, I thought that I could use the child node attributes themselves to store things with some generics magic.
So my quadtree its like:
class QuadTree
{
QuadTree Norheast, Southeast, Southwest, Northwest;
}
Each QuadTree object has four more QuadTree objects, until I reach the leaves, then I initialize those with null, and the time to store something comes. My idea was that those variables could be more nodes of class QuadTree<T> or objects that I want to store, depending on the location of the node that I'm creating. Something like:
if (nextNodeisNotLeaf) //Next level on the tree is not the last.
{
// Each attribute is a new QuadTree object, and the process repeats.
NE = new QuadTree();
SE = new QuadTree();
SW = new QuadTree();
NW = new QuadTree();
else
{
// Its a leaf, so each attribute is of the class type I want to store.
NE = new objT();
SE = new objT();
SW = new objT();
NW = new objT();
But I don't know how to do that. Its like a recursive thing, except I need to unroll the whole thing on a single line if I want to do anything! (ie, quad tree of 4 levels: root.NE = quadtree<quadtree<objectToStoreType>>; and what if I want to make a 5 level quad tree later on the program? Can't modify the class's source on runtine! ).
I wan't to decide what type each object instance of that class is going to store. But can't figure it out how to do it all in a single generic class...