Find a pointer in a vector?

Hello,

I'm working on building a game engine and I'm having a bit of an issue trying to use a get function to return a pointer to an object.

I have an object manager class which has a member vector that manages the game assets. I've created a Player class and added it into the vector, now from a different class I'm trying to call the object managers get function to search the vector and return the pointer to whatever class object I'm passing in as an parameter, unfortunately I'm confused on the syntax (coming from AS3 programming background).

It looks something like this:

1
2
3
4
5
6
7
8
9
CEntityManager* CObjectManager::Get(//not sure what to as parameter something like: CEntityManager * obj) const
{
	std::vector<CEntityManager*>::const_iterator it = _mEntity.begin();
	for (it; it != _mEntity.end(); it++)
	{
		std::cout << (*it) << std::endl;
	}
	return NULL;
}


I'm calling it with a dynamic_cast like this:

//CPlayer* player = dynamic_cast<CPlayer*>(CGame::GetManager().Get(player));

but that doesn't work properly...but that's very similar how I would do it in AS3.
Last edited on
I understand nothing :( It looks horrible.
Maybe you should use associative arrays? http://www.cplusplus.com/reference/stl/map/
unless your CEntityManager has an overloaded equality operator, you will need to pass in some unique identifier to search for.

We cant pass in the object we are looking for, if we could then we wouldnt have to find it in the first place!

I suggest a hash or name string to identify each CEntityManager in the vector. I put in place some temporary values for the data types, but here is what I think you might be needing.

BTW, your code above should be displaying the pointers to the CEntityManager objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// consider replacing UniqueEntityManagerMemeberType with "std::string"
// and targetProperty/targetPropertyValue with "name"

CEntityManager* CObjectManager::Get(const UniqueEntityManagerMemberType & targetPropertyValue) const
{
	// a vector of CEntityManager pointers
	std::vector<CEntityManager*>::const_iterator it = _mEntity.begin();
	
	// a CEntityManager pointer for temporary use
	CEntityManager * pEntMgr;
	
	for (it; it != _mEntity.end(); it++)
	{
		// grab the pointer behind this iterator
		pEntMgr = *it;
		
		// dereference the CEntityManager pointer
		// and compare its targetProperty to our targetPropertyValue
		if ( (*pEntMgr).targetProperty == targetPropertyValue )
			// if this is the value we want, then
			// return the pointer to this CEntityManager object
			return pEntMgr;
	}
	return NULL;
}
@bradw -

That seems very logical, actually I'm not sure why I didn't think of that before. I guess I'm used to using class types as searchable values. I'll give this a try, but it looks like it will do the trick since I use a similar property to disable drawing and remove pointers. Going to mark this as solved.

Thanks again!
Topic archived. No new replies allowed.