/* Function Object */ #include #include using namespace std; /* function will not work - can not hold value between calls. void f(int i){ int hold; hold += i; } */ // use this object fadd to implement a function like object // that can hold values between calls. // fadd, since it overloads the () operator, is called a // function object. class fadd{ int hold; public: fadd():hold(0){} int operator()(int a){hold += a; return hold;} }; int main(int nargs, char **args){ int x = 3; fadd f; // declare an instance of fadd - create an object. cout << f(x) << endl; cout << f(4) << endl; return EXIT_SUCCESS; }