Front link and pop_front

Is this legal ?:

Sample& sample = stack.front(); stack.pop_front(); 

My program is working. But the Sample class has a member boost::optional<boost::posix_time::ptime> xxx and after pop_front , is_initialized() returns false;

+4
source share
2 answers

No, this is not legal. You must take a copy of the object, i.e. Use

 Sample sample = stack.front () 

If you use std::vector , calling pop_front moves the elements behind the place, and your landmarks point to another element (previously the second, now the first element.)

+10
source

No, of course not. Once you remove an object from the collection, nothing is referenced in the link.

+1
source

All Articles