Skip to main content
GameDev.net gamedev.net
🔒 Locked

Need help with returning a value from a std::list boost::shared_ptr

Started by chadsxe Feb 25, 2009 at 12:38 PM 3 replies 550+ views
Original Post
chadsxe
chadsxe
I am just starting to learn how to use boost::shared_ptr and had a few questions about how to return a proper value from an std::list when the value of that list is a boost::shared_ptr.
// "Mesh" is a class
typedef boost::shared_ptr<Mesh> MeshPtr;
std::list< MeshPtr > m_meshList;
//1) I think I want to return that object?
Mesh Graphics::GetMesh( const std::string &meshName )
{
	std::list< MeshPtr >::iterator listIterator;
	// Find the font. 
	for( listIterator = m_meshList.begin(); listIterator != m_meshList.end(); listIterator++ )
	{       // 2) is wrapping listIterator in (* ) correct? 
		if( (*listIterator)->GetMeshName() == meshName )
		{
                        // 3) This obviously does not work
			return *listIterator;
		}
	}
	return Mesh();
}

1) I am little bit confused as to what the return object should be. I orginally been using this functio to return Mesh objects out of a std::list of Mesh object. Not shared_ptr. And it work fine by returning a Mesh object. 2) Am I suppose to wrap my iterator in (*) to access the functions of the object it is pointing to? 3) I am not sure what the return value would be. Regards Chad
Zakwayda
Zakwayda
In the example you posted, the return type should be MeshPtr. (Remember that dereferencing an iterator returns a reference to an object of the element type, and the element type of the container in question is MeshPtr.)

Keep in mind that the caller will acquire a shared reference to the mesh object rather than a copy of the mesh object, but with this sort of system that's generally what you want. (If for some reason you actually do want a copy, you can acquire one using a double dereference.)

Also, consider using std::find() and a function object rather than a hand-written loop to locate the requested mesh. (Or, better yet, us a map rather than a list, which will make it easier to map names to mesh objects.)
rip-off
rip-off
Quote:
Original post by jyk
Also, consider using std::find() and a function object rather than a hand-written loop to locate the requested mesh. (Or, better yet, us a map rather than a list, which will make it easier to map names to mesh objects.)


This has been mentioned this to chadsxe a few times.

Because the hint isn't enough, let us consider what the code would look like if you were using std::map:
// header#include <map>#include <string>class Graphics{	typedef std::map<std::string, MeshPtr> MeshMap;public:	MeshPtr GetMesh(const std::string &);	// ...private:	MeshMap meshMap;};// source fileMeshPtr Graphics::GetMesh( const std::string &meshName ){	MeshMap::iterator it = meshMap.find(meshName);	if(it == meshMap.end())	{		return MeshPtr();	}	else	{		return it->second; // it->first is the string.	}}

This is exactly the use that std::map<> is intended for.
chadsxe
chadsxe
Thanks to all for the info.

@ rip-off
I apologize a million times if it appears I am not taken the advice given. This was not my intention at all. I have taken notes of all the suggestions handed out to me and fully plan on implementing things when the oppurtunity is correct. I just feel like with me in the middle of refactoring some code I had written a while ago and the fact that I am still learning what methods would work in my my overall design of this project that I need to take babysteps through the development process. Again I apologize. Thanks for the example. It will be of great use.


Regards

Chads
rip-off
rip-off
Hey, no need to apologise. Better to make progress on your project than try and take every piece of advice that might be thrown at you. I just wanted to throw that example in to give you some incentive to learn more about std::map<>, I was hoping the simplicity might appeal to you [smile]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.