tags:

views:

129

answers:

1

I was wondering what would be the best way to accomplish something like this...

// Iterating through a list
if ( foo ) {
  RemoveBar( it ); 
}

void RemoveBar( std::list< Type >::iterator it ) {
  it = listName.erase( it );
  ...// Other stuff related to cleaning up the removed iterator
}

I don't think pass by value will work here. Obviously what I want to do is stay in the correct iterator position when I call RemoveBar. Is pass by reference the best alternative?

+5  A: 

Iterators are normally passed by value. I'd make RemoveBar() return the new iterator.

anon
That was the original intent, however I prefer it when function names reveal what their intent is, and RemoveX doesn't reveal that it's going to return an iterator, and RemoveXAndGetX is meh, but since it's a private function I guess I can bend the readability rules a bit.
Anonymous
Dores that mean that you think printf() should really be called print_and_return_number_of_characters_output() - not many would agree, I think :-)
anon