Tuesday, July 31, 2007

Arrays of Function Pointers in C++

So imagine you have a function called test_function_ref in a class called DLMAP. In another function in the same class, you want to have an array of function pointers that includes test_function_ref, and then you want to call it using the array of function pointers. This is how you would do that:

void (DLMAP::*fptr[2])(BitStream &theBits);
// set up an array of functions that return void and take a reference
// to a BitStream object as an argument
fptr[1] = &DLMAP::test_function_ref;
// assign the 1'th element of the function pointer array to the address of the function
(*this.*fptr[1])(theBits);
// now, actually call the function using the this pointer


Wasn't that fun? I got the info here.

No comments: