an example of function object


👍 g++ funcobj.cpp && ./a.out
39
39
👍 cat funcobj.cpp 
#include <iostream>
using namespace std;

class classf {
public:
  classf() {
  }
  double operator() (double x) {
    return 3 * x + 7;
  }
};

double sum(classf f, int n, int m) {
  double result = 0;
  for (int i = n; i <= m; i++)
    result += f(i);
  return result;
}

int main() {
  classf cf;
  cout << sum(cf, 1, 3) << endl;
  cout << sum(classf(), 1, 3) << endl;
}