C++ vector reference
c++, vector
Solution
Is the reference c still valid (or better yet, is it guaranteed to be valid)?
Nope it is now in an undefined state.
If not, do I have to always make copy from the reference to ensure safety?
If there is a possibility of it being deleted or moved (etc) between your retrieving it and your using it, then yes you DO need to make a copy.
Problem
I'm confused about how C++ manages objects in vector. Say I do the following: ``` vector<MyClass> myVector; myVector.push_back(a); myVector.push_back(b); MyClass & c = myVector[1]; myVector.erase(myVector.begin()); ``` Is the reference c still valid (or better yet, is it guaranteed to be valid)? If not, do I have to always make copy from the reference to ensure safety?