Function pointer
Encyclopedia
A function pointer is a type of pointer in C
, C++
, D
, and other C-like programming languages, and Fortran
2003. When dereferenced
, a function pointer can be used to invoke a function
and pass it 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 pointers do always point to a function having a specific signature. Thus, all functions used with the same function pointer must have the same parameters and return type.
. They are also used as callback functions if it is necessary to use a member function as a callback function.
Many "pure" object-oriented languages 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 function
s, 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.
. 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:
Although function pointers in C and C++ can be implemented as simple addresses, so that typically
.
External links
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
, C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, D
D (programming language)
The D programming language is an object-oriented, imperative, multi-paradigm, system programming language created by Walter Bright of Digital Mars. It originated as a re-engineering of C++, but even though it is mainly influenced by that language, it is not a variant of C++...
, and other C-like programming languages, and Fortran
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...
2003. When dereferenced
Dereference operator
The dereference operator or indirection operator, denoted by "*" , is a unary operator found in C-like languages that include pointer variables. It operates on a pointer variable, and returns an l-value equivalent to the value at the pointer address. This is called "dereferencing" the pointer...
, a function pointer can be used to invoke a function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....
and pass it 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 pointers do always point to a function having a specific signature. Thus, all functions used with the same function pointer must have the same parameters and return type.
Functors
Functors, or function objects, 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 closuresClosure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...
. They are also used as callback functions if it is necessary to use a member function as a callback function.
Many "pure" object-oriented languages do not support function pointers. Something similar can be implemented in these kinds of languages, though, using references
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...
to interfaces
Protocol (object-oriented programming)
In object-oriented programming, a protocol or interface is a common means for unrelated objects to communicate with each other. These are definitions of methods and values which the objects agree upon in order to cooperate....
that define a single member function. Microsoft .NET languages such as C# and Visual Basic .NET
Visual Basic .NET
Visual Basic .NET , is an object-oriented computer programming language that can be viewed as an evolution of the classic Visual Basic , which is implemented on the .NET Framework...
implement type-safe
Type safety
In computer science, type safety is the extent to which a programming language discourages or prevents type errors. A type error is erroneous or undesirable program behaviour caused by a discrepancy between differing data types...
function pointers with delegates
Delegate (.NET)
A delegate is a form of type-safe function pointer used by the .NET Framework. Delegates specify a method to call and optionally an object to call the method on. They are used, among other things, to implement callbacks and event listeners....
.
In other languages that support first-class function
First-class function
In computer science, a programming language is said to have first-class functions if it treats functions as first-class objects. Specifically, this means that the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning...
s, 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.
Method pointers
C++ is object-oriented, so classes can have methodsMethod (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...
. Non-static member functions (instance methods) have an implicit parameter (the this
This (computer science)
In many object-oriented programming languages, this is a keyword that is used in instance methods to refer to the object on which they are working. C++ and languages which derive in style from it generally use 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 inheritanceVirtual inheritance
Virtual inheritance is a topic of object-oriented programming. It is a kind of inheritance in which the part of the object that belongs to the virtual base class becomes common direct base for the derived class and any next class that derives from it...
.
External links
- FAQ on Function Pointers, things to avoid with function pointers, some information on using function objectFunction objectA function object, also called a functor, functional, or functionoid, is a computer programming construct allowing an object to be invoked or called as though it were an ordinary function, usually with the same syntax.-Description:...
s - Function Pointer Tutorials, a guide to C/C++ function pointers, callbacks, and function objectFunction objectA function object, also called a functor, functional, or functionoid, is a computer programming construct allowing an object to be invoked or called as though it were an ordinary function, usually with the same syntax.-Description:...
s (functors) - Generic Function Pointers In C And Void *, Why can't void * be used as a generic function pointer in C
- Member Function Pointers and the Fastest Possible C++ Delegates, CodeProject article by Don Clugston
- Pointer Tutorials, C++ documentation and tutorials
- Secure Function Pointer and Callbacks in Windows Programming, CodeProject article by R. Selvam