, - ?
#include <iostream>
#include <iterator>
struct Iterable {
virtual int current() = 0;
virtual void advance() = 0;
protected:
~Iterable() {}
};
struct Iterator : std::iterator<std::input_iterator_tag,int> {
struct Proxy {
int value;
Proxy(const Iterator &it) : value(*it) {}
int operator*() { return value; }
};
Iterable *container;
Iterator(Iterable *a) : container(a) {}
int operator*() const { return container->current(); }
Iterator &operator++() { container->advance(); return *this; }
Proxy operator++(int) { Proxy cp(*this); ++*this; return cp; }
};
struct AbstractStorage : private Iterable {
Iterator iterate() {
return Iterator(this);
}
virtual ~AbstractStorage() {}
};
struct ConcreteStorage : AbstractStorage {
int i;
ConcreteStorage() : i(0) {}
virtual int current() { return i; }
virtual void advance() { i += 10; }
};
int main() {
ConcreteStorage c;
Iterator x = c.iterate();
for (int i = 0; i < 10; ++i) {
std::cout << *x++ << "\n";
}
}
- Iterator::operator== Iterator::operator-> ( , ).
ConcreteStorage, , . , , Iterable, Storage, Iterable Storage. , , , Iterable, shared_ptr ( Itertable , newIterator shared_ptr, ).