- [Instructor] In C++ you may take the address
of a function and assign it to a pointer.
This is called a function pointer.
Function pointers are a bit rare,
but they're extremely useful in some circumstances.
So let's see how this works
and then we'll look at an example.
This is func.cpp from chapter five of the exercise files.
And we have a simple function here
that just prints this out.
It's a void function, so it doesn't return anything.
And then down here in main, we call that function.
But instead of calling the function,
let's create a function pointer.
And it would look like this, the function returns void.
So the function pointer returns void
and we'll call it pfunc,
which will be the name of the pointer.
And we'll assign the address of func
which is not taken with the address of operator.
And you notice the parentheses here,
that's what tells us that it's a function pointer
and you notice that the function pointer is
in parentheses and that has to do with operator precedence.
So those parentheses are necessary there.
And we call it simply like this.
And now when I build and run,
you'll notice that it calls a function just
as if we had called it directly.
Now it's possible,
and you'll see this sometimes,
where you can simply call it with the name
of the function pointer
as if it were the name of the function.
And when we build and run this,
you'll notice that it works just fine.
This is ambiguous and so this usage is not recommended.
It's much better practice to show
that you're calling a function pointer.
So let's take a look at a practical application here.
Go ahead and delete this
and run clean and take jump.cpp
and copy that into my working project.
And we'll take a look at this program.
Here you'll see we have one, two, three,
four, five functions.
And these are just simple functions that put a string
on the console.
And they're named f, a, b, c, d, e, like that.
And then we have here an array of function pointer.
So here we have our function pointer syntax,
with the brackets for an array.
So it's an array of function pointers,
and we assign as the values,
the addresses of these five functions.
Now we have a couple of utility functions.
One is to put a prompt on the screen and return a response
and we'll see how that works in a minute.
And then down here we have what I like to call a jump table.
What this does is it takes that response
from the prompt function, which is a character string,
a C string, and it checks for q.
And if it gets a q which is quit, then I return zero,
otherwise it says, okay, so how big is our array?
And it takes that first character
from the string, it subtracts.
This is a little trick.
If you subtract the character zero from a character,
you get a numeric value from the ASCII numeral.
That's just a little ASCII trick from long, long ago.
And we check and make sure that it's in range.
And if it is, we call the function from the array like that.
And we call this in main, just like this,
we call prompt and we pass its value right there
into the parameter for jump.
And when I build and run this,
you notice that here I get our prompt string.
And if I select number one, I get, this is fa
which is the fa function.
And if, say, I say, number three,
I get this fc, which is the third option.
And if I press q, you notice I get done.
I have exited everything and that done is for main
when we get out of the while loop.
So function pointers can be very useful.
You're not going to need them often,
but when you do, they're extremely convenient.