foreach.cpp 772 Bytes
#include <stddef.h>
#include <iostream>

class Iterator;

class Collection
{
  static const size_t size = 10;
  int data[size];
  using iterator = Iterator;
  friend class Iterator;
public:
  Iterator begin();
  Iterator end();
};

class Iterator 
{
  int i;
  Collection* c;
public:
  Iterator(int ii, Collection* cc) : i(ii), c(cc){};
  int& operator *()
  {
    return c->data[i];
  }
  Iterator& operator++()
  {
   ++i;
   return *this;
  }
  bool operator!=(const Iterator& o)
  {
   return i != o.i;
  }

};

Iterator Collection::begin()
{
  return Iterator(0,this);
}

Iterator Collection::end()
{
  return Iterator(Collection::size-1,this);
}

int main()
{
Collection c;

int a = 3;

for(auto& i: c)
  i = a++;

for(auto i: c)
  std::cout << i << std::endl;

}