Mateusz Loskot :: hacking on, working out, living up

vector of function pointers

06 Mar 2009 | mloskot

This is just a small tip on how to define a vector of function pointers in C++ and how to call functions stored in it:

<code>#include <vector>
#include <iostream>

typedef int (*fptr)(int, int);
int foo(int a, int b) { return a + b; }
int bar(int a, int b) { return a * b; }

int main()
{
    std::vector<fptr> f;
    f.push_back(foo);
    f.push_back(bar);

    std::cout << f[0](2, 3)
        << f[1](2, 3)
        << std::endl;
    return 0;
} </code>

That’s it. Next, this example could be extend to replace old-fashion function pointers with function objects.

Fork me on GitHub