【发布时间】:2016-04-15 20:43:56
【问题描述】:
人们会期望它们以完全相同的方式工作,但是。
class Range
{
public:
class DontMindMe
{
public:
DontMindMe(int a) : a_(a) {}
bool operator ==(const DontMindMe& ot) {return a_ == ot.a_;}
bool operator !=(const DontMindMe& ot) {return !(*this==ot);}
int operator *(void) {return a_;}
DontMindMe& operator++() {++a_; return *this;}
DontMindMe operator++(int) {auto temp{*this}; ++a_; return temp;}
private:
int a_;
};
Range(int a, int b, const std::string& msg)
: a_(a), b_(b) {std::cout << msg << std::endl;}
DontMindMe begin() {return a_;}
DontMindMe end() {return b_;}
private:
int a_;
int b_;
};
int main()
{
for(auto it = Range::DontMindMe(1); it != Range(1,10, "C++").end(); ++it);
std::cout << std::endl;
for(auto it : Range(1,10, "C++11"));
}
第一个循环打印消息 10 次,第二个循环只打印一次。他们不是平等的吗?这种差异会使旧代码修订复杂化吗?
【问题讨论】: