Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Function pointer
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Functors== {{main|Function object}} 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 [[closure (computer programming)|closures]]. They are also used as callback functions if it is necessary to use a member function as a callback function.<ref>{{cite web | access-date = 2011-04-13 | date = 2005-01-31 | publisher = DevX.com | title = Expertise: Intermediate Language: C++: Use Functor for Callbacks in C++ | quote = 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]. | url = http://www.devx.com/tips/Tip/27126}}</ref> Many "pure" object-oriented languages do not support function pointers. Something similar can be implemented in these kinds of languages, though, using [[reference (computer science)|references]] to [[protocol (object-oriented programming)|interfaces]] that define a single [[method (computer programming)|method]] (member function). [[List of CLI languages|CLI languages]] such as [[C Sharp (programming language)|C#]] and [[Visual Basic .NET]] implement [[type safety|type-safe]] function pointers with [[delegate (CLI)|delegate]]s. 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 a [[branch predictor]] 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 summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)