1

Trying overloading operator() in the following example:

#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;
}

It prints the fib sequence in the reverse order as 8,5,3,2,1,1. I understand the states are kept in () overlading but why the printing is showing up in the reverse order?

KKG10
  • 11
  • 2

1 Answers1

1

operator << is a some function defined for its parameters. The order of evaluation of function arguments is unspecified. They can be evaluated right to left or left to right. It seems your compiler evaluates them right to left.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268