title: function pointer vs functors in C++
body: <p>What is the difference between using functors and function pointers.
  For example</p>
  
  <pre><code>  //Functor
    struct add_x
    {
      int x;
      add_x(int y):x(y){}
      int operator()(int y)
      {
         return x+y;
      }
    };
    //Function
    int (func)(int x)
    {
       return ++x;
    }
    std::vector&lt;int&gt; vec();
    //fill vec with 1 2 3 4 5
    int (*f)(int) = func;//Function pointer
    std::transform(vec.begin(),vec.end(),f); //approach 1
    std::transform(vec.begin(),vec.end(),add_x(1)); //approach 2
  </code></pre>
  
  <p>Both approaches work but i am sure there will be cases where one is preferred(or possible) over other.</p>