Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created June 30, 2015 15:24
Show Gist options
  • Save krysseltillada/ab4cdb465936c27f9654 to your computer and use it in GitHub Desktop.
Save krysseltillada/ab4cdb465936c27f9654 to your computer and use it in GitHub Desktop.
storing a number of functions in a vector that returns a function pointer and accessing them
#include <iostream>
#include <vector>
int display (int, int);
int sum (int n1, int n2)
{
return n1 + n2;
}
int subtract (int n1, int n2)
{
return n1 - n2;
}
int multiply (int n1, int n2)
{
return n1 * n2;
}
int divide (int n1, int n2)
{
return n1 / n2;
}
int (*func(int (*f) (int , int ))) (int, int)
{
std::cout << __func__ << " is called " << std::endl;
return f;
}
int main()
{
std::vector<int (*)(int, int)> vec_func;
vec_func.push_back(func(sum));
std::vector<int (*) (int, int)>::iterator it = vec_func.begin();
for (unsigned i = 0; it != vec_func.end(); ++it, ++i)
std::cout << vec_func[i](22, 22) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment