Making a std::vector of function pointers



Hello, I'm working on a little project where I need instances of my Actor class to have a sort of scriptable function execution order. My impulse is to have each instance of Actor have a unique std::vector of function pointers. My intention is to allow each instance to execute their functions in a different order and number of times. I'm totally not sure how this would be constructed as I've not yet had to use function pointers (they are often not taught in courses it seems). Is there a better way to have an instance call it's functions in an arbitrary order which are set after instanciation?
Last edited on
There is a good tutorial located on this site that could help you further you knowledge and hopefully help you do what you want. Sorry if it did not help, its the only answer i had.
std::vector<std::function>
I'm not sure if you're using member or non-member functions. Here's an example with both.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <vector>

class AClass
{
public:
	long f(int, float) { return 0; }
};

long f(int, float)
{
	return 0;
}

typedef long func_t(int, float);
typedef func_t* pfunc_t;
typedef long (AClass::*pmemfunc_t)(int, float);

typedef std::vector<pfunc_t> funcvec_t;
typedef std::vector<pmemfunc_t> memfuncvec_t;

int main()
{
	funcvec_t mv;
	mv.push_back(f);

	memfuncvec_t fv;
	fv.push_back(&AClass::f);

	return 0;
}
Thank you all so much for the help!
It has been greatly appreciated.
Topic archived. No new replies allowed.