Thursday, August 16, 2007

What Is a Function Pointer

What Is a Function Pointer ?

Function pointers are nothing but a pointer only Or we can say it is a pointer variable which points to the address of a function.

How To Define a Function Pointer ?
As I rightly told that it is a simply pointer variable , so we can declare it as usual manner.
So we can declare it both in C and C++.In C++ ,it points to member function of the class or a const member function of the class .

First we will discuss in C , then in C++

Ptf= pointer to function
Ptmf= pointer to member function
Ptcmf = pointer to const member function

int (*ptf)(int) = NULL; // in C
int (MyClass::*ptmf)(float, char) = NULL; // C++
int (MyClass::*ptcmf)(int) const = NULL; // C++

So in general we can say it is something like :
return type (* function pointer name ) (arguments);
As we know once we declare a variable , generaly we initialise it to NULL or 0 depending on type of variable... :)
So it is a good practice to initialize function pointers with NULL.

No comments: