/*
    Functors vs. functions
*/

#include <iostream>
using namespace std;

// define classical function
double func(double x, double a)
{
// Lets say f(x,a) = g(a)*h(x) where g(a) is very expensive to calculate.
//    
   
   double ga=10*a;   // How to avoid evalating this every time the function is called.
   double hx=x;
   
   return ga*hx;
}



// define class with a functor interface.
class Functor
{
   double ga;  // local cache
   
 public:
   Functor(double a)
      {
         // here we calculate ga once and cache the value for all subsequent calls.
         ga=10.0*a;
      }
      
   double operator()(double x)
      {
         double hx=x;
         return ga*hx;
      }
};



int main()
{
    cout << "f(1,pi)=" << func(1.0, 3.1415) << endl;
    
    Funktor F(3.1415);  // When F is defined Functor(a) is called.
    cout << "f(1,pi)=" << F(1.0) << endl;
}


