Jump to content
  • Advertisement
Sign in to follow this  
Marx2052

A quick iterator question

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

I have a reference handed into my function like this: ConstructList(const std::vector<CObject*> &pObjectList) The vector being referenced is a vector of pointers to the CObject class. I've been tring to make an iterator for this reference using the following code: std::vector<CObject*>::iterator IterObject; However when I try to use the iterator it only lets me access the following data (by the way this is while in the editor not while running) for (IterObject = pObjectList.begin() + 1; IterObject < pObjectList.end(); IterObject++) { //brings up information about the actual iterator operators and such IterObject. //allows me to access the class that the iterator should be pointing to IterObject[0]-> } Surely I shouldn't need to put the array [] operators since the iterator should be a 'pointer' as such to the actual CObject pointer contained in the vector. Any ideas why its acting like this, it's probally I simple mistake but I'm still getting to grips with the std library. Thanks Marx!

Share this post


Link to post
Share on other sites
Advertisement
The iterator is basically a pointer to an element in your vector. Since an array is just a pointer to the first element in the array, you can use "[0]" to access the actual data pointed to by the iterator (the element in the vector). So since in your case the element is another pointer, you can dereference it using the "->" operator.

It's perfectly normal C++ behavior. For example, you can do this:


int* numPtr = new int;
*numPtr = 5;
cout << numPtr[0]; // Will output "5"


Of course doing so would probably be considered abuse of the language. If it's just a pointer you're working with and not an array, you should dereference with the "*" operator.


Btw...why did you post this in DirectX? [smile]

Share this post


Link to post
Share on other sites
If you are passing in a const vector, I don't understand how you can assign to anything other than a const_iterator? I would have thought you'd get a confusing error.


void f(const std::vector<int> &v)
{
std::vector<int>::iterator i=v.begin(); // error
std::vector<int>::const_iterator i=v.begin(); // okay
}

Share this post


Link to post
Share on other sites
You dereference your iterator using e.g. *iterator. That gives you access to the thing the iterator points at. However in your case the iterator is pointing at a pointer, so you can think of it as a double-dereference e.g. (*(*iterator)) So in this case for accessing the methods of the pointed-at object, a pointer, you would use -> i.e. (*iterator)->doWhatever()

edit: @ EasilyConfused, true 'dat, but he's not even got to compiling yet! I think this is something that's related to intellisense not offering the method list he's expecting.

Share this post


Link to post
Share on other sites
Quote:
MJP said: Since an array is just a pointer to the first element in the array
not true, close but not quite.

just to reiterate MJP, you need to dereference twice. you are passing a "reference to an array of pointers" to the method.

Firstly, the reference is implicitly dereferenced, so you may ignore it for the most part since it is const as well).

The iterator encapsulates the vector traversal to appear like an array, by using the "->" operator on the iterator you are doing the first explicit dereference, the operator now returns "an element in the array at the given iterator position".

That element is of the vector's template argument type i.e. CObject*. clearly, this is also a pointer.

Since "->->" is not valid syntax, you must dereference the iterator first using the asterisk notation, i.e. (*iter).

Now you have a valid pointer to a CObject hence you can do this (*iter)->do_stuff().

Phew.. hope that made sense..

Share this post


Link to post
Share on other sites
Nice! the one that worked was

(*Iterator).function

I had tried *Iterator.function but that never worked I suppose it makes sense since if I use the . operator straight after the iterator it would consider it as its own .operator and present me with the function of the iterator container but with the brackets it preform the .operator on the pointer contained within it.

Thanks loads!
Marx!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!