C & CPP Function Pointers
C & CPP Function Pointers
1/5 2007-09-02
Function Pointer
Much like other large information containers, the function name is a container
for the function start address. When calling a function we actually call for transferring
the execution flow to the address contained by the function name.
Pointer declaration:
When we declare a function’s pointer, we declare the address container and
the function’s signature. The declaration starts with the return type, then the container
name preceded by the pointer indicator enclosed in parenthesis indicating it is a
pointer not function name then the parameters type list. When no parameter type is
specified in the inclosing, the pointer indicates unspecific number of parameters.
Return_Type ( *Func_Pointer_Name ) (List_Prameter_Types)
Declare return type. Declare pointer to function. Declare list of parameter types.
Like pointers to variables, Pointers to functions may point to different functions. But
the pointed function signature must always be in correspondence with the pointer
declaration.
Declaration Example: void (*FuncPtr1)(); int (*FuncPtr2)(int, float, int);
Like any pointer it is possible to create multiple pointers to the same address there for
allowing multiple pointers to be used for function call.
MyFunc() points to
MyFunc(…)
{…}
(*Pmy_func) points to
C++: When declaring a class function pointer it is no longer enough to declare the
function signature, the declaration must include the class reference.
int (TMyClass::*pt2Member)(float, char, char) = NULL;
Operators:
Functions and function pointers support logical operators like any other
container. It is possible to compare function address with function pointer or to get the
function return value by using the function pointer.
Comparing with the function -
If(pMyFunction == &MyFunction){…/*pointer points to the function*/}
Get the return value using the “*” operator.
Data_Container = (* Func_Pointer_Name) (Parameters_List);
Example:
Int MyFunc (int x, float y, int z) {
return(x*y*z);
};
void main(){
int Var1;
int (*FuncPtr2)(int,float,int);
FuncPtr2=MyFunc;
Var1 = (*FuncPtr2) (3, 3.115, 4);
}
C++:
The function is referenced threw the class there for the function name must include
the scope operator “::”.
if(pMyFunction == &TMyClass::MyFunction){
cout << "Pointer points to TMyClass::MyFunction" << endl;
Passing as argument:
The target function must have a function pointer as target and the calling
function pass the pointer using the referencing operator(with out parameters list).
Int MyFunc(float num1, char num2, char num3){…}
void PassPtr(int (*pMyFunc)(float, char, char)){
int result = (*pMyFunc)(12,'a','b');
}
void Pass_A_Function_Pointer(){
PassPtr(&DoIt);
}
Returning as value:
For declaring a function returning a function pointer the function declaration is
replaces the function pointer name in the pointer declaration. The function declaration
template is <ret val> (*<pointer name>)(<parameters stamp>), there for the function
declaration will be:
float (*GetPointerToFunc(const char opCode))(float, float)
{
if(opCode == '+') return &Plus;
else return &Minus; // default if invalid operator was passed
}
Or for declaring function returning a function pointer (much simpler than standard
declaration.
typedef float(*TypePMyFunc)(float, float);
TypePMyFunc GetMyFuncPointer(const char opCode){
if(opCode == '+') return &Plus;
else return &Minus;
}
C++:
The function array must reference the function class and be part of the object
otherwise it must contain a reference to the function object.
void Array_Of_Member_Function_Pointers()
{
int (TMyClass::*funcArr2[10])(float, char, char) = {NULL};
funcArr1[0] = &TMyClass::DoIt;
funcArr1[1] = &TMyClass::DoMore;
TMyClass instance;
cout << (instance.*funcArr1[1])(12, 'a', 'b') << endl;
cout << (instance.*funcArr1[0])(12, 'a', 'b') << endl;
}
// Using typedef.
typedef int (TMyClass::*pt2Member)(float, char, char);
void Array_Of_Member_Function_Pointers()
{
pt2Member funcArr1[10] = {NULL};
:
}