| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Function_pointer |
| Contents |
|---|
| Functors |
| PL/I |
| See also |
| References |
| External links |
A function pointer, also called a subroutine pointer or procedure pointer, is a pointer referencing executable code, rather than data. Dereferencing the function pointer yields the referenced function, which can be invoked and passed arguments just as in a normal function call. Such an invocation is also known as an "indirect" call, because the function is being invoked indirectly through a variable instead of directly through a fixed identifier or address.
Function pointers allow different code to be executed at runtime. They can also be passed to a function to enable callbacks.
Function pointers are supported by third-generation programming languages (such as PL/I, COBOL, Fortran, dBASE dBLfunc1 takes one double-precision (double) parameter and returns another double, and is assigned to a function which converts centimeters to inches.
Although function pointers in C and C++ can be implemented as simple addresses, so that typically sizeof(Fx) == sizeof(void*), member pointers in C++ are sometimes implemented as "fat pointers", typically two or three times the size of a simple function pointer, in order to deal with virtual methods and virtual inheritance.// This defines 'C', a class with similar static and member functions, // and then creates an instance called 'c' classC{ public: staticintStatic(charc); intMember(charc); }c;// C // This defines 'p', a pointer to 'C' and assigns the address of 'c' to it C*p=&c; // This assigns a pointer-to-'Static' to 'fn'. // Since there is no 'this', 'Fn' is the correct type; and 'fn' can be used as above. fn=&C::Static; // This defines 'm', a pointer-to-member-of-'C' with type 'Fn', // and assigns the address of 'C::Member' to it. // You can read it right-to-left like all pointers: // "'m' is a pointer to member of class 'C' of type 'Fn'" FnC::*m=&C::Member; // This uses 'm' to call 'Member' in 'c', assigning the result to 'cA' intcA=(c.*m)('A'); // This uses 'm' to call 'Member' in 'p', assigning the result to 'pA' intpA=(p->*m)('A'); // This defines 'Ref', a function that accepts a reference-to-'C', // a pointer-to-member-of-'C' of type 'Fn', and a 'char', // calls the function and returns the result intRef(C&r,FnC::*m,charc){ return(r.*m)(c); }// Ref(r, m, c) // This defines 'Ptr', a function that accepts a pointer-to-'C', // a pointer-to-member-of-'C' of type 'Fn', and a 'char', // calls the function and returns the result intPtr(C*p,FnC::*m,charc){ return(p->*m)(c); }// Ptr(p, m, c) // LEGACY: Note that to maintain existing code bases, the above definition style can still be used first; // then the original type can be defined in terms of it using the new style. // This defines 'FnC', a type of pointer-to-member-of-class-'C' of type 'Fn' typedefFnC::*FnC; // 'FnC' can be used wherever 'Fn C::*' can FnCfnC=&C::Member; intRefP(C&p,FnCm,charc);
PL/I procedures can be nested, that is, procedure A may contain procedure B, which in turn may contain C. In addition to data declared in B, B can also reference any data declared in A, as long as it doesn’t override the definition. Likewise C can reference data in both A and B. Therefore, PL/I ENTRY variables need to contain context, to provide procedure C with the addresses of the values of data in B and A at the time C was called.
| View original on Wikipedia | Back | FazBrowse Home |