What is the difference between using functors and function pointers. For example
//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<int> 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
Both approaches work but i am sure there will be cases where one is preferred(or possible) over other.