Hi Relfos,
You need to adopt (or at least I think you do) similar methods to TCollection.
When you create an instance of TCollection, you'd do something like this:-
Code:
myCollection:=TCollection.create(TMyCollectionItemClass);
TMyCollectionItemClass is normally descended from TCollectionItem. Once you've done this, you just use the generic 'add' method of the collection to create a new node, like this:-
Code:
var
myNewItem : TMyCollectionItemClass;
begin
myNewItem:=TMyCollectionitemClass(myCollection.add);
end;
You can implement this yourself using class references (I think thats the term). The key stages are as follows.
Define your base octree item class
Code:
TOctreeNode = class(TObject)
....
end;
Then define the type that provides (I think) the class reference you will use in the octree object.
Code:
TOctreeNodeClass = class of TOctreeNode;
Now define your octree object.
Code:
TOctree = class(TObject)
protected
fNodeClass : TOctreeNodeClass;
...
public
constructor create(nodeClass:TOctreeNodeClass);
function add:TOctreeNode;
end;
In the constructor, keep a record of the class reference you've passed to it.
Code:
constructor TOctree.create(nodeClass:TOctreeNodeClass);
begin
inherited;
fNodeClass:=nodeClass;
end;
The add method then looks something like this.
Code:
function TOctree.add:TOctreeNode;
begin
result:=fNodeClass.create;
end;
This example may not hold up with exact syntax etc. and obviously you will have to store the new nodes internally etc. but I hope it gives you the idea of how this kind of thing is handled in collections (and I hope its what you wanted).
Once you've got the base classes working, complete with the base properties you require, then whenever you need to store something different, you just create a new class descended from TOctreeNode. It does have the overhead of typecasting, but I think it will serve your purposes.
Bookmarks