In my class, I have two private members:
std::list<MyObject> objects; MyObject *selecteObj;
When an event occurs, I would like to iterate through the list and run some kind of test that will only give true for one of the elements in the list. I would like to keep a pointer to this element for use elsewhere.
std::list<MyObject>::iterator i; for (i = objects.begin(); i!=objects.end(); ++i){ if (i->test()) selectedObj = i; }
In another place In another method
if (selectedObj !=null) tmpObj->doSomething();
However, this does not work, because i not a pointer, its iterator, even if you can consider it as a pointer to MyObject .
Is it possible to retrieve the pointer that the iterator stores internally for use elsewhere?
Am I thinking about it wrong?
What is the correct way to accomplish what I'm trying to do?
source share