3

Is it safe to do the following?

std::list<Something> someList;

...
someList.push_back(Something(8));
Something* something = &someList.back();

I would think it is safe but I'm not completely sure.

Thanks

jmasterx
  • 47,653
  • 87
  • 281
  • 523
  • Even though that's valid code, if you're using raw pointers to reference things in C++ you might want to consider a different mechanism, such as using an iterator or a reference. – fluffy Jun 19 '12 at 05:28

1 Answers1

3

Yes the object will be valid until you erase it from the someList. See Iterator Invalidation Rules for information about when the objects can get destroyed for various container operations.

Community
  • 1
  • 1
Naveen
  • 69,046
  • 43
  • 164
  • 225