Sign in to follow this  
NIm

arrays of functions [c++]

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
No, it's not necessary, but it's good habit, since the & is required when you take the address of member functions.

Share this post


Link to post
Share on other sites
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_1
func_vec.push_back(new func_ai_1);

// then, you just call operator() to call the functor

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this