Should I write iterators for a class that is just a wrapper of a vector?

Viewed 481

Should I write iterators for a class that is just a wrapper of a vector?

The only private member of my class called Record is a vector.

I want to be able to do this:

for (auto& elem : record) {
    // do something with elem
}

where record is of type Record. To do this I need to implement iterators for the Record class. But, I can also do this:

for (auto& elem : record.elems) {
    // do something with elem
}

where record.elems is the vector I mentioned. But this way I will need to make it public.

Another approach is:

for (auto& elem : record.getElems()) {
    // do something with elem
}

this way I get iteration, and the member stays private. Probably this is the best way to go.

My main question is, why would I bother to implement iterators for the Record class when it is just a wrapper for a vector? My colleague insists I implement iterators, and he can't even explain me why, it is just stupid.

2 Answers
Related