n****e 发帖数: 10 | 1 class Iterator {
public:
Iterator() : cur(0) {};
bool hasNext() {
return cur < 20;
};
int next() {
++cur;
return cur;
};
private:
int cur;
};
int main() {
Iterator it;
int a = it.next();
int b = it.next();
cout << a << " " << b << endl;
cout << it.next() << " " << it.next() << endl;
}
为啥输出是
1 2
4 3
而不是
1 2
3 4 |
f*******w 发帖数: 1243 | 2 因为cout是从右往左读到buffer里面,然后再从左往右输出的
所以先执行后面那个next()而不是前面那个 |
M********0 发帖数: 1230 | 3 编译器的问题
gcc 给出的是
1 2
4 3
clang和icpc给出的是
1 2
3 4 |
f*******w 发帖数: 1243 | 4 这个还真不知道……涨姿势了
【在 M********0 的大作中提到】 : 编译器的问题 : gcc 给出的是 : 1 2 : 4 3 : clang和icpc给出的是 : 1 2 : 3 4
|
t**8 发帖数: 4527 | 5 nonsense question
【在 n****e 的大作中提到】 : class Iterator { : public: : Iterator() : cur(0) {}; : bool hasNext() { : return cur < 20; : }; : int next() { : ++cur; : return cur; : };
|
n****e 发帖数: 10 | 6 Thanks for all the answers. It does not have much algorithmic stuff, but
reminds me of the arguments pushing order of functions, and different
implementation choices for different compilers. |
n****e 发帖数: 10 | |