This question already has answers here:
cout << order of call to functions it prints?

(3个答案)



Undefined behavior and sequence points

(5个答案)


5年前关闭。




在以下示例中尝试重载operator():
#include <iostream>
using namespace std;

class Fib {
  public:
    Fib() : a0_(1), a1_(1) {}
    int operator()();
  private:
    int a0_, a1_;
};
int Fib::operator()() {
    int temp = a0_;
    a0_ = a1_;
    a1_ = temp + a0_;
    return temp;
}

int main() {
    Fib fib;

    cout << fib() <<"," << fib() << "," << fib() << "," << fib() << "," << fib() << "," << fib() << endl;
}

它以相反的顺序打印fib序列为8,5,3,2,1,1。我知道状态始终处于()重叠状态,但是为什么打印却以相反的顺序显示?

最佳答案

运算符<

关于c++ - 以相反的顺序进行cout打印,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29525549/

10-11 22:07
查看更多