Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sdoherty55

Referencing STL Iterator Pointers

This topic is 5913 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is probably pretty easy, but I can''t seem to figure out how to reference the STL iterator pointer for my list? Thanks
  
typedef list<CShip> CShipList;

CShipList::iterator p;
for (p=m_oShipList.begin(); p!=m_oShipList.end(); p++){
   if (hr = p->Render() != D3D_OK){
	return hr;
   } 
   if (p !=  pPlayerShip){  // How do I reference the pointer?


  

Share this post


Link to post
Share on other sites
Advertisement

  
//Is this a typo?

typedef list<CShip> CShipList;

// Did you mean?

typedef list<CShip *> CShipList;


You can use the unary * (deference operator) to get back
the original CShip * from the iterator, assuming you are
storing CShip * in the first place.

*p will do.

Edit: added source tag

~~~~
Kami no Itte ga ore ni zettai naru!

[edited by - tangentz on July 17, 2002 6:17:18 PM]

Share this post


Link to post
Share on other sites
No,

  
typedef list<CShip> CShipList;


was not a typo.

For some reason, I implemented CShip as a list of object, but implemented CExplosion as a list of pointers.


  
typedef list<CExplosion *> CExplosionList;


With out converting CShip to a list of pointers can I accesss there pointers?

Share this post


Link to post
Share on other sites

          

typedef list<CShip> CShipList;

CShipList::iterator p;

for (p = m_oShipList.begin(); p != m_oShipList.end(); ++p)
{
if (hr = p->Render() != D3D_OK)
return hr;

if (&(*p) != pPlayerShip)
{
// if the pointers are not equal

}
}



Is that what you want? (assuming the & operator for CShip isnt overloaded, otherwise you can use boost::addressof).

And you may wish to look at std::for_each for iterating through elements of an STL container....


  

#include <algorithm>
#include <functional>

for_each(m_oShipList.begin(), m_oShipList.end(), mem_fun_ref(CShip::Render));



I think that should work. I havent tried compiling anything mind.

[edit] - forgot #include <functional>

[edited by - sark on July 17, 2002 6:51:54 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!