accessing members when they're stored in lists

hello again, i've started looking at STL lists for the first time. I'm starting an exercise i found on this web site about mutant bunnies that breed(good exercise actually..)! .

Anyway, i would like to store my bunny objects to a list( i considered vectors, but i think lists would be better as i have to insert and delete perhaps in the middle of the list?). What i would like to know is, how i access the members of the mutantType when they are stored in list? See my attempt below which doesn't work!!!. printSummary() is a member of mutantType class.


std::list <mutantType*> bunnyList;

bunnyList.push_front( new mutantType );
bunnyList.push_front( new mutantType );
bunnyList.push_front( new mutantType );
bunnyList.push_front( new mutantType );

std::list < mutantType* > ::iterator iElementLocator;

for ( iElementLocator = bunnyList.begin();
iElementLocator != bunnyList.end();
++ iElementLocator )

bunnyList->printSummary();
Try (*bunnyList)->printSummary();

Don't forget that you store pointers in your list -> std::list <mutantType*> bunnyList;
This means you need two derefernce operations, one for the iterator and one for the pointer.
Last edited on
Oh.. have i made a mistake?. Shouldn't the last line be

*iElementLocator->printSummary();

it's the iterator that steps through the list, isn't it?

line above still doesn't work though!!
Oh, yeah... I made a mistake too... that's what I meant -> (*iElementLocator)->printSummary();

Mike200 wrote:
line above still doesn't work though!!

You forgot the parentheses :P
(*iElementLocator)->printSummary();

this works.. thanks
Topic archived. No new replies allowed.