例如,是否应将以下类分为两部分:
class Range {
public:
Range(int end);
Range begin();
Range end();
// Second class would contain these methods.
bool operator!=(const Range& range);
int operator*();
void operator++();
};
// Print 012
// Almost as pretty as Python :D
for (int i : Range(3)) {
std::cout << i;
}
?
PS:标准库中有这样的类吗?
更新:
以下是替代设计的外观:
class Range;
class RangeIterator {
public:
bool operator!=(const RangeIterator& range_iterator);
int operator*();
void operator++();
private:
RangeIterator(const Range& range);
};
class Range {
public:
Range(int end);
RangeIterator begin();
RangeIterator end();
friend class RangeIterator;
};