I have a dynamic array wrapper template like this:
class Wrapper {
public:
T* const start;
T* const end;
T* begin() const { return start; }
T* end() const { return end; }
/*
more code
*/
};
which gives value and reference access via the loops:
Wrapper<T> wrapper;
for(auto val : wrapper) {
//do smth to value
}
for(auto& ref : wrapper) {
//do smth to reference
}
. I now want to create a range based for loop which is equivalent to this:
for(auto ptr = wrapper.start; ptr != wrapper.end; ptr++) {
//do smth to pointer
}
, i.e. I want a range based loop over wrapper to give access to a pointer. Is there a way to do this without creating an array of pointers to pointers inside my wrapper?
Edit:
Dani's solution in the comment works as has already been pointed out here. I was actually wondering if there was a way to make the following syntax:
for(auto ptr : wrapper) {
//do smth
}
work as the C-style for loop above.
for(auto& ref : wrapper) { auto ptr = &ref; }? Can I somehow alter thebegin()andend()methods to get this working implicitly insite this:for(auto ptr : wrapper) { /* do smth */ }?