面白いなぁ。
#include <iostream> using namespace std; class Fib { private: int a; int b; public: Fib() : a(1), b(1) {} int operator() (){ int tmp = a; a = b; b = tmp + b; return tmp; } }; int main() { Fib fib; for (int i = 0; i < 10; i++) { cout << fib() << endl; } return 0; }
1
1
2
3
5
8
13
21
34
55