There is no correct answer. The answer will vary depending on the context, of which we currently have very few.
It depends on the customers of your class. In most situations, you do not need a random inspector to change the state of the object, so to some extent it is better to return a reference to the const object. However, in this case, I would also make a const i.e.
const vector<int> & GetPointsVector() const;
This is also effective since you don't go around heavy objects as return values ββ(this is another question that most compilers do RVO these days).
If your client needs to use a vector in algorithms, yes, you better provide iterators. But two pairs, i.e.
typedef vector<int>::iterator _MyItr; _MyItr begin() const; _MyItr begin(); _MyItr end() const; _MyItr end();
This will be consistent with how the STL is designed. But be careful when specifying which iterator the client can expect (for example: if the class specification says the returned iterators are RandomIterators , it sets a certain number of expectations in your implementation).
source share