object iteration

Started by
3 comments, last by Agony 17 years, 8 months ago
Suppose, in C++, I have an object of type CScene. CScene can contain objects that have implemented the class CSceneObject. How do I iterate through ALL scene objects to use the functions or properties of the base class? (The base class contains information to use in collision detection. To test a wall against a camera, wich ofcause are of different type) Any comment would be helpfull and appreciated. Dennis
Advertisement
If using inheritance and the base class function has not be overwritten then the base class function will be called.
----------------------------

http://djoubert.co.uk
That's right, but I want to itterate through all different subclasses.
You may introduce a class CSceneGroup that inherits CSceneObject and can contain several other CSceneObject instances. Notice please that CSceneGroup is able to contain other CSceneGroups since it is derived from CSceneObject. This is called the composite pattern. You may derive CScene from CSceneGroup if you want. The only drawback is that theoretically you can add another CScene into an existing CScene. That may or may not be senseful.

Now, having the above structure, you can implement routines that perform iteration. You may implement a functional purpose into this routine, but then you will end up implementing dozens of such routines (e.g. one for finding a node by name, one for extracting bounding boxes, one for BSP extraction, one for whatever else, ...). Normally it is better to implement the purpose in an external class, and let the composite structure only drive the iteration. E.g. the base class of all those algorithmic classes may be called CSceneVisitor. So the CSceneObject class may provide a routine CSceneObject::visitingDepthFirst(CSceneVisitor* visitor) for a depth first iteration, and so on. Then the visitor may provide a CSceneVisitor::visiting(CSceneObject*).

This concept has the drawback that a visitor has to determine the concrete type of any visited CSceneObject. However, it is nowadays consensus that nodes of a scene graph should not be specialized by inheritance too much. Instead, collaborators/aggregation should be the way of choice. Said so, it is relatively simple for visitors to ask visited nodes for special kind of aggregates. E.g. CSceneObject::getLocalBBox(). Making the bbox global may then be the task of the visitor.


Hope that this is something in the direction you are looking for?!
Quote:Original post by MarauderX
That's right, but I want to itterate through all different subclasses.


You say that "CScene can contain objects that have implemented the class CSceneObject." What method do you use to do this? Do you use an array? std::vector? std::list?

Also, how do you create your CSceneObject instances? Do you use new, and thus obtain pointers?

The general way to do this is to have a container of CSceneObject*. Then, just go through the whole container, and call the functions that you need.

//Initialization of a std::vector<CSceneObject*> called mSceneObjects:mSceneObjects.push_back(new CSceneObject_Box(1, 1, 1, 0, 0, 0));mSceneObjects.push_back(new CSceneObject_Box(1, 1, 1, 10, 0, 0));mSceneObjects.push_back(new CSceneObject_Box(20, 1, 20, 0, -1, 0));mSceneObjects.push_back(new CSceneObject_Monster(0, 1, 0));//Iteration later on:std::vector<CSceneObject*>::iterator iObject;for (iObject = mSceneObjects.begin(); iObject = mSceneObjects.end(); ++iObject){  (*iObject)->Update(Time);}//Alternate iteration using indices:std::vector<CSceneObject*>::size_type iObject;for (iObject = 0; iObject < mSceneObjects.size(); ++iObject){  mSceneObjects[iObject]->Update(Time);}//Cleanup:std::vector<CSceneObject*>::iterator iObject;for (iObject = mSceneObjects.begin(); iObject = mSceneObjects.end(); ++iObject){  delete *iObject;}mSceneObjects.clear();


"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement