Sign in to follow this  

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

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

This topic is 3041 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.

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