Function pointer

Function pointer

A function pointer is a type of pointer in C, C++, D, and other C-like programming languages, and Fortran 2003.[1] 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.[2]

Contents

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 closures. They are also used as callback functions if it is necessary to use a member function as a callback function.[3]

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 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.

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[citation needed].

References

  1. ^ "The Function Pointer Tutorials". http://www.newty.de/: logo. http://www.newty.de/fpt/intro.html#what. Retrieved 2011-04-13. "Function Pointers are pointers, i.e. variables, which point to the address of a function" 
  2. ^ "The Function Pointer Tutorials". http://www.newty.de/: logo. http://www.newty.de/fpt/intro.html#top. Retrieved 2011-04-13. "Important note: A function pointer always points to a function with a specific signature! Thus all functions, you want to use with the same function pointer, must have the same parameters and return-type!" 
  3. ^ "Expertise: Intermediate Language: C++: Use Functor for Callbacks in C++". http://www.devx.com/: DevX.com. 2005-01-31. http://www.devx.com/tips/Tip/27126. Retrieved 2011-04-13. "If you want to use a member function as a callback function, then the member function needs to be associated with an object of the class before it can be called. In this case, you can use functor [with an example on this page]." 

See also

External links


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Pointer (computing) — This article is about the programming data type. For the input interface (for example a computer mouse), see Pointing device. Pointer a pointing to the memory address associated with variable b. Note that in this particular diagram, the computing …   Wikipedia

  • Function object — A function object, also called a functor or functional, is a computer programming construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax.Function objects are unrelated to functors in… …   Wikipedia

  • function object — noun An object that encapsulates a function pointer (or equivalent). Syn: functor …   Wiktionary

  • Function prologue — In assembly language programming, the function prologue is a few lines of code which appear at the beginning of a function, which prepare the stack and registers for use within the function. Similarly, the function epilogue appears at the end of… …   Wikipedia

  • Function prototype — A function prototype in C or C++ is a declaration of a function that omits the function body but does specify the function s name, arity, argument types and return type. While a function definition specifies what a function does, a function… …   Wikipedia

  • Pointer analysis — In computer science pointer analysis, or points to analysis, is a static code analysis technique that establishes which pointers, or heap references, can point to which variables or storage locations. It is often a component of more complex… …   Wikipedia

  • Pointer (Informatik) — Mit Zeiger oder Pointer wird in der Informatik eine spezielle Variable bezeichnet, die auf eine andere Variable oder Funktion verweist. Der referenzierte Speicherbereich enthält entweder Daten (Objekt, Variable) oder Programmcode. Zeiger auf… …   Deutsch Wikipedia

  • pointer — I. /ˈpɔɪntə / (say poyntuh) noun 1. someone or something that points. 2. a long, tapering stick used by teachers, lecturers, etc., in pointing things out on a map, blackboard, or the like. 3. → forefinger. 4. the hand on a watch, machine, or… …  

  • First-class function — In computer science, a programming language is said to support first class functions (or function literal) if it treats functions as first class objects. Specifically, this means that the language supports constructing new functions during the… …   Wikipedia

  • Dangling pointer — Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. These are special cases of memory safety violations. Dangling Pointer Dangling pointers arise when an object is… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”