Function pointer
From Wikipedia, the free encyclopedia
This article does not cite any references or sources. Please help improve this article by adding citations to reliable sources (ideally, using inline citations). Unsourced material may be challenged and removed. (May 2007) |
A function pointer is a type of pointer in C, C++, D, and other C-like programming languages. When dereferenced, a function pointer invokes a function, passing it zero or more arguments just like a normal function. In programming languages like C, function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.
Function objects, or functors, are similar to function pointers, and can be used in similar ways. A functor is an object of a class type that implements the function-call operator, allowing the object to be used within expressions using the same syntax as a function call. Functors are more powerful than simple function pointers, being able to contain their own data values, and allowing the programmer to emulate closures, among other uses.
Many "pure" object-oriented languages (such as Java) do not support function pointers. Something similar can be implemented in these kinds of languages, though, using references to interfaces that define a single member function. Microsoft .NET languages such as C# and Visual Basic .NET implement type-safe function pointers with delegates.
In other languages that support first-class functions, functions are regarded as data, and can be passed, returned, and created dynamically directly by other functions, eliminating the need for function pointers.
Extensively using function pointers to call functions may produce a slow-down for the code on modern processors, because branch prediction may not be able to figure out where to branch to (it depends on the value of the function pointer at run time) although this effect can be overstated as it is often amply compensated for by significantly reduced non indexed table lookups.
[edit] Method pointers
C++ is object-oriented, so classes can have methods. Non-static member functions (instance methods) have an implicit parameter (the this pointer) which is the pointer to the object it is operating on, so the type of the object must be included as part of the type of the function pointer. The method is then used on an object of that class by using one of the "pointer-to-member" operators: .*
or ->*
(for an object or a pointer to object, respectively).
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 often implemented as "fat pointers", typically two or three times the size of a simple function pointer, in order to deal with virtual inheritance (see also virtual function).
[edit] External links
- Generic Function Pointers In C And Void *, Why can't void * be used as a generic function pointer in C
- Pointer Tutorials, C++ documentation and tutorials
- Pointer Tutorials, pointer basics
- Function Pointer Tutorials, a Guide to C/C++ function pointers, callbacks, and functors
- Member Function Pointers and the Fastest Possible C++ Delegates, CodeProject article by Don Clugston.
- FAQ on Function Pointers, things not to do with function pointers, some information on using Functionoids.