arrays of functions [c++]

Started by
6 comments, last by Mushu 18 years, 7 months ago
I am using c++, and I am wondering whether it is possible to have an array of functions. it would be convenient because I have a bunch of seperate ai functions, and a different one needs be used for each type of creature. having an array of them would be alot easier and more efficient than using pointers. If it is possible to use arrays of functions, please let me know.
Advertisement
It's not possible to have an array of functions, but you can have an array of function pointers. For example:
typedef void (*func_ptr)(void);func_ptr array[5] = { &func_a, &func_b, &func_c, &func_d, &func_e};


However, you may want to investigate function objects, and in particular, boost::function.
I realize C and C++ are not entirely the same but, on many fronts, it is. On that note, I don't think the '&' is necessary for each function you need the address of since; an function automatically exposes its address with just its name. Correct me if I'm wrong.

relient
No, it's not necessary, but it's good habit, since the & is required when you take the address of member functions.
Native function pointer syntax is crap, in my opinion.

I suggest that you either look at some third-party function pointer libraries (boost::function, fast_delegate, loki maybe?), or learn functors.

Here's a link for boost::function:

boost::function

I couldn't find the fast delegate library, or if loki had support for functors.

You can do functors yourself by merely wrapping up a virtual function in an abstract class, and defining new functions by deriving off that base class:
class functor {     public:          virtual void operator()() = 0;};class func_ai_1 : public functor {     public:          virtual void operator()() {               // do whatever          }};// store functors in a vector:std::vector<functor*> func_vec;// add func_ai_1func_vec.push_back(new func_ai_1);// then, you just call operator() to call the functor
Sounds to me like you could have the AI as a class and make each creature its own class that inherits the AI class.
Quote:Original post by MastaKilla
Sounds to me like you could have the AI as a class and make each creature its own class that inherits the AI class.


I was thinking the same thing. OP: Look up virtual functions and how to use inheritance. I think this would be the most elegant solution to your problem.
another solution might be to use callbacks:

struct cCallback_ {	virtual void call() {};};template <class T> class cCallback : public cCallback_ {protected:	T* obj;	void (T::*func)(void);public:	cCallback(T* obj, void (T::*func)(void)) {		this->obj = obj; this->func = func; }	virtual void call() {		(obj->*func)(); }};


You'd use it by having an array of cCallback_ pointers, which point to cCallback instances (yay polymorphism) -
std::vector<cCallback_*> funcs;funcs.push_back(new cCallback<myObj>(myObj::func, myObj_instance)); 

The above isn't a really safe way to do it, because you're not guarenteeing that the memory will be deallocated. But whatever.

You can make it really sexy by making both the base class and the derived callback templated, so that you can specify any number of arguments/return values of any type [wink]

Though I think I'd have to agree with MastaKilla that just using inheritance between your creature classes would far simplify things, and be a lot cleaner.

This topic is closed to new replies.

Advertisement