Is it possible to create an "external" iterator class (ie: without access to the original data type)

Viewed 107

I am working on a project where it would be useful to create an iterator type for an MFC class CArray.

Since CArray is part of the MFC libraries I do not have access to this object.

I have not completely got my head around creating iterators, but as far as I am aware, one would usually create an iterator type as a member of the class type for which the iterator corresponds. In other words, access to the class itself is required.

Is it possible to create an iterator "external" to a class. ie: External to the CArray class.

1 Answers

CArray is a continuous container. Thus, a simple pointer is a random-access iterator for this container:

template<class ...Ts>
auto begin(CArray<Ts...>& arr) noexcept { return &arr[0]; }

template<class ...T>
auto begin(CArray<Ts...> const& arr) noexcept { return &arr[0]; }

template<class ...T>
auto end(CArray<Ts...>& arr) noexcept { return begin(arr) + arr.GetSize(); }

template<class ...T>
auto end(CArray<Ts...> const& arr) noexcept { return begin(arr) + arr.GetSize(); }
Related