Skip to content

Instantly share code, notes, and snippets.

@froydnj
Created June 15, 2015 23:57
Show Gist options
  • Save froydnj/27d62104d4a71b38af60 to your computer and use it in GitHub Desktop.
Save froydnj/27d62104d4a71b38af60 to your computer and use it in GitHub Desktop.
#include <stdio.h>
class Table {
friend class Iterator;
public:
Table(int x, int y, int z, int w)
{
mArray[0] = x;
mArray[1] = y;
mArray[2] = z;
mArray[3] = w;
}
class Iterator {
friend class Table;
public:
Iterator(Iterator&& aOther)
: mCurrent(aOther.mCurrent),
mEnd(aOther.mEnd) {
aOther.mCurrent = 0;
aOther.mEnd = 0;
}
bool Done() const { return mCurrent == mEnd; }
void Next() { ++mCurrent; }
int Get() const { return *mCurrent; }
private:
Iterator(Table* aTable) {
mCurrent = &aTable->mArray[0];
mEnd = &aTable->mArray[3] + 1;
}
private:
int* mCurrent;
int* mEnd;
Iterator() = delete;
Iterator(const Iterator&) = delete;
Iterator& operator=(const Iterator&) = delete;
};
Iterator Iter() { return Iterator(this); }
private:
int mArray[4];
};
void
Example()
{
Table t(6, 11, 21, 18);
for (auto iter = t.Iter(); !iter.Done(); iter.Next()) {
int x = iter.Get();
printf("%d\n", x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment