Jump to content
  • Advertisement
Sign in to follow this  
Slyfox

Using iterators to access member functions of a pointer stored in a vector

This topic is 3229 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

Hi, I've been playing around with vectors and iterators and have run across a problem. I've created a vector of pointers of a custom type as follows
std::vector<AnimatedPlaneModel*> m_pVecClouds;

I've then created an iterator to use with that vector:
std::vector<AnimatedPlaneModel*>::iterator iterClouds;

Later in my code I then wish to iterate through the vector and pass values into the member functions of the class AnimatedPlaneModel such as ->SetPosition(0.0f, 0.0f, 0.0f); However I cannot figure out how to access the member functions of the pointer pointed to by the iterator. Here is an example of the code I tried...
for(iterClouds = m_pVecClouds.begin(); iterClouds != m_pVecClouds.end(); iterClouds++ )
{
     // The following line does not compile
     *iterClouds->SetPosition(0.0f, 0.0f, 0.0f);
}

I understand that by derefencing the iterator I am retrieving the element that the iterator is referencing, but cannot figure out why I cannot access the member functions of the pointer. I have found one way to get round this problem which is to use a temporary pointer as follows...
AnimatedPlaneModel* pAPMTemp = *iterClouds;
pAPMTemp->SetPosition(0.0f, 0.0f, 0.0f)

This fixes the problem, but I was wondering if there was a way to achieve this without the need for the temporary variable?

Share this post


Link to post
Share on other sites
Advertisement
It's most likely a problem with precedence. Try:
(*it)->DoSomething();

Share this post


Link to post
Share on other sites
Ah excellent, thanks very much - I had a feeling it would turn out to be something brilliantly simple cheers!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!