Can You Make Arrays Of Functions?


zacaj

void main()
Joined
Apr 3, 2007
Messages
362
Age
29
Location
NY
Website
zacaj.com
Hi,

I have 6 different functions, that each draw a different version of something, depending on what type it is. Right now I have a switch statement, jsut calling the right funtion, and I was wondering if there was a way to just call eg. draw[type](); or something along those lines to draw it.

Thanks
 
You mean like this:

typedef void (*myfunc)(void);
myfunc Funcs[] = {funcname1, funcname2, etc}

Funcs[arraypos]();

Assuming a function that takes no arguments and returns nothing. Edit as appropriate :)
 
I would say the easiest solution would be to write one generic function that takes the 'type' as a parameter. Even if it means having the switch case within the function itself, just have a mix of generic and non-generic code in the function. I would just create an enum for the different types.
 
Funcptr's for the win :)

(This is exactly how C++ compilers do it, as well as function callback systems and so on. A very useful technique, but you must be careful to make sure all the functions have a compatible retuyrn value and param list..)

jeff
 
Back
Top