This is a subtle one...
If you were to return a copy and not a reference like this vector<int> getVector(){return vec;}
This this line for(vector<int>::const_iterator beg=begin(obj.getVector());beg!=end(obj.getVector());beg++ is a BIG problem because:
beg=begin(obj.getVector()) will be pointing to one vector copy
but will be testing for the end of the vector against end(obj.getVector()) which is another seperate vector copy.
These are TWO DIFFERENT vectors - (or more to the point - TWO different copies of obj.vec).
[[I would have thought though that the program would print out for a very long time and possible crash as the end iterator would never match with beg unless by pure luck]]
That is why returning the reference works - because then the two interators beg and end will be testing against the SAME vector variable.