Sign in to follow this  
Kimochi

Differentiate between object types in std::list

Recommended Posts

Hi there, I have the following classes (simplification):
SceneManager
{
public:
	SceneManager();
	virtual ~SceneManager();

	/* loads of functions etc. */

	std::list<SceneNode*>* GetBaseNodes();
protected:
	std::list<SceneNode*> BaseNodes;
};



class SceneNode
{
public:
	SceneNode();
	virtual ~SceneNode();

	/* loads of functions etc. */
};



class SpecificNode: public SceneNode
{
public:
	SpecificNode();
	virtual ~SpecificNode();

	int SPECIFICFUNCTION();
};



And I'm currently iterating through all the SceneNodes in the SceneManager using this:
std::list<SceneNode*>* BaseNodes = mSceneManager->GetBaseNodes();

for(std::list<SceneNode*>::iterator Node = BaseNodes->begin(); Node != BaseNodes->end(); ++Node)
{
	/* loads of code */

/* PSEUDOCODE
	if(type of Node == "SpecificNode")
	{
		(*Node)->SPECIFICFUNCTION();
	}
*/

	/* loads of code */
}



Currently I am using the GetBaseNodes() function to return the BaseNodes variable. While this works well, there is currently no way to call SPECIFICFUNCTION on nodes of type SpecificNode within the list. Is there a way to iterate through the BaseNodes list and only call SPECIFICFUNCTION on nodes of type SpecificNode? I'd really like to avoid putting SPECIFICFUNCTION into the SceneNode class as a virtual function, as this would mean defining SPECIFICFUNCTION in classes not using it. Thanks a lot!

Share this post


Link to post
Share on other sites
You could dynamic_cast the pointer to your SpecificNode type; however, you really should see if there's some higher level functionality covered by your loop iteration that can be factored into your base class definitions.

Share this post


Link to post
Share on other sites
specific_node* mynode = dynamic_cast<specific_Node*>(general_Node_ptr)
if(mynode != NULL)
{
//call function on mynode.
}

Ought to do the trick. dynamic_cast does have performance related concerns so I wouldn't go overboard with it. Needing to use it often is a sign of a poorly thought out design(aka the big bag o crap design pattern).

Share this post


Link to post
Share on other sites
Thanks for the responses. I will give dynamic casting a try.

The last code section I posted was part of the rendering routine, which is called once per frame. And, seeing as there could potentially be thousands of SceneNodes of type SPECIFICNODE on screen at a time, performance is a huge concern.

Would it be better to create a separate list of pointers within the SceneManager, specifically to contain objects with function SPECIFICFUNCTION?

(edit: Haha - That 'bag of crap' metaphor was brilliant. It seems I am indeed using a 'big bag o crap' design approach here.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this