Created
June 15, 2015 23:57
-
-
Save froydnj/27d62104d4a71b38af60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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