/*
    Functors vs. functions

    Functors looks like functions, but they have memory!
*/

#include <iostream>
#include <cmath>
using namespace std;

// define classical function, no way to store data from previous calls
double func(double x, double a)
{
    return a*sin(x);
}



// define class with a functor interface.
class Functor
{
    double a;      // Amplitude
    int calls;     // How many times have we called operator()

public:
    Functor(double aa) : a(aa), calls(0)  {   }

    double operator()(double x)
    {
        calls++;
        return a*sin(x);
    }

    int GetCalls() { return calls; }
};



int main()
{
    // Call to function func
    cout << "func(1)=" << func(1.0, 12.0) << endl;

    // Definition of Functor F with operator()
    Functor F(12.0);
    cout << "Functor(1,pi)=" << F(1.0) << endl;
    cout << "Functor(2,pi)=" << F(2.0) << endl;
    cout << "Functor(3,pi)=" << F(3.0) << endl;
    cout << "Calls to functor " << F.GetCalls() << endl;
}


