In my class I have two private members:
std::list<MyObject> objects;
MyObject *selecteObj;
When an event occurs I’d like to iterate through the list and run some sort of test that will only yield true on one of the items in the list. I’d like to save a pointer to that item for use elsewhere.
std::list<MyObject>::iterator i;
for (i = objects.begin(); i!=objects.end(); ++i){
if (i->test())
selectedObj = i;
}
Elsewhere In another method
if (selectedObj !=null)
tmpObj->doSomething();
However this doesn’t work because i isn’t a pointer, its an iterator even though you can treat it like a pointer to an MyObject.
Is it possible to retrive the pointer that the iterator is storing internally for use elsewhere?
Am I thinking about this incorrectly?
What is the proper way of accomplishing what I’m trying to do?
Sure, just dereference the iterator with
*and then get a pointer to the object via&:On a side note, did you remember to initialize
selectedObjwithNULLbefore the loop?