cout以相反的顺序打印

时间:2015-04-08 21:19:43

标签: c++ function-object

在以下示例中尝试重载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。我知道状态保持在()重叠,但为什么打印以相反的顺序出现?

1 个答案:

答案 0 :(得分:1)

运营商&lt;&lt;是为其参数定义的某个函数。函数参数的评估顺序未指定。它们可以从右到左或从左到右进行评估。看起来你的编译器从右到左评估它们。