Anda di halaman 1dari 5

Itai Danielli @.

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;

C:\_Studies\4. Computers\3.3. Basics of C & Cpp\9. C & Cpp Function pointers.rtf


Itai Danielli @. 2/5 2007-09-02

Assigning a function to a pointer:


Since a function name contains the function address, simply assign a function
name (with no parameters) to a pointer. Another more widely used syntax is using the
address operator befor the function name.
pMyFunction = MyFunction; // short syntax.
pMyFunction = &MyFunction; // Widely used syntax.
C++: Function pointer can be assigned only to C style functions, which are Global
or Static member functions. Pointers to none static functions require a pointer to the
object from which they are instantiated. Use global wrapper function and a static
access function for directing the pointer to the object.
MyClass{
Bool MyFunc(<parameters>);
Public: static bool MyFuncAccess(void* pObj,<parameters>);
};
bool MyClass ::MyFuncWrapper(void* pObj,bool(*pMyFunc)<parameters>);

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++:

C:\_Studies\4. Computers\3.3. Basics of C & Cpp\9. C & Cpp Function pointers.rtf


Itai Danielli @. 3/5 2007-09-02

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
}

Defining a function pointer type:


Function pointer type defines a function pointer as a new type, which can be
used like any other container type. In the following example we will use function,
which returns an enumerator type and receives void pointer to a memory block
containing the function parameters.
Typedef enum {FALSE = 0, TRUE=1}RESULT;
typedef RESULT (*p_function)(void* p_param);
The type can be used for declaring function pointer as structure member
typedef struct{
p_function my_function;
RESULT res;
}S_MY_FUNC;

C:\_Studies\4. Computers\3.3. Basics of C & Cpp\9. C & Cpp Function pointers.rtf


Itai Danielli @. 4/5 2007-09-02

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;
}

Calling the function:


Function pointer can be used like any function name, calling the function can be done
with the function call syntax. This mechanism is similar to using a pointer as an array
name.
int result1 = pMyFunction(<parameters>); // C short way
The standard way of using the function pointed by the pointer is by de-referencing it,
using the “*” operator.
int result2 = (*pt2Function) (12, 'a', 'b'); // C
C++:
The function is part of an object, when the function pointer is a member of the
instance the “*” operator is used.
TMyClass instance1;
int result3 = (instance1.*pt2Member)(12, 'a', 'b'); // C++

TMyClass* instance2 = new TMyClass;


int result4 = (instance2->*pt2Member)(12, 'a', 'b');
delete instance2;

Function pointers array:


The function pointers array can be implemented directly or using the type
definition. It is much simpler and there for recommended to use the type definition.
The function array is a fast an efficient way to implement operation code activation.
void Array_Of_Function_Pointers()
{
int (*funcArr2[2])(float, char, char) = {NULL};
funcArr1[0] = funcArr2[1] = &DoIt;
funcArr1[1] = funcArr2[0] = &DoMore;
printf("%d\n", funcArr1[1](12, 'a', 'b')); // short.
printf("%d\n", (*funcArr1[0])(12, 'a', 'b')); // "correct".
}
typedef int (*pt2Function)(float, char, char);
void Array_Of_Function_Pointers()
{
pt2Function funcArr1[10] = {NULL};
:
}

C:\_Studies\4. Computers\3.3. Basics of C & Cpp\9. C & Cpp Function pointers.rtf


Itai Danielli @. 5/5 2007-09-02

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};
:
}

C:\_Studies\4. Computers\3.3. Basics of C & Cpp\9. C & Cpp Function pointers.rtf

Anda mungkin juga menyukai