I have a the following code.
vector<IRD>* irds = myotherobj->getIRDs();//gets a pointer to the vector<IRD>
for(vector<IRD>::iterator it = irds->begin(); it < irds->end(); it++)
{
IRD* ird = dynamic_cast<IRD*>(it);
ird->doSomething();
//this works (*it).doSomething();
}
This seems to fail…I just want to get the pointer to each element in the vector without using (*it). all over.
- How do I get the pointer to the object?
- When I iterate over the vector pointer irds, what exactly am I iterating over? Is it a copy of each element, or am I working with the actual object in the vector when I say (*it).doSomething(),
Why do you want to get a pointer?
Use a reference:
Alternatively:
Also, as everyone said, use
!=when comparing iterators, not<. While it’ll work in this case, it’ll stop working if you use a different container (and that’s what iterators are for: abstracting the underlying container).