Complex manifold: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>EmausBot
m r2.7.3) (Robot: Adding ja:複素多様体
 
en>Nbarth
Line 1: Line 1:
[http://www.Tumblr.com/tagged/Selected Selected] of the writer is generally Adrianne Quesada. Managing people today is what she is performing in her day job but she's always wanted her own business. Her husband doesn't like it then the way she must but what she especially likes doing is so that you can bake but she's believing on starting something newbie. Vermont are where her house was. Her husband and her maintain a website. You may want to check it out: http://circuspartypanama.com<br><br>
A '''function pointer''' (or '''subroutine pointer or procedure pointer''') is a type of [[pointer (computer programming)|pointer]] supported by [[third-generation programming language|third-generation]] [[programming language]]s (such as [[PL/I]], [[COBOL]], [[Fortran]],<ref>{{cite web
| accessdate = 2013-09-14
| location = http://www.esm.psu.edu/~ajm138/fortranexamples.html
| title = Fortran Examples
| author = Andrew J. Miller
| url = http://www.esm.psu.edu/~ajm138/fortranexamples.html#ex1
}}</ref> dBASE dBL, and [[C (programming language)|C]]) and [[object-oriented programming]] languages (such as [[C++]] and [[D (programming language)|D]]).<ref>{{cite web
| accessdate = 2011-04-13
| location = http://www.newty.de/
| publisher = logo
| title = The Function Pointer Tutorials
| quote = Function Pointers are pointers, i.e. variables, which point to the address of a function
| url = http://www.newty.de/fpt/intro.html#what}}</ref> Instead of referring to data values, a function pointer points to executable code within memory. When [[dereference operator|dereferenced]], a function pointer can be used to invoke the [[subroutine|function]] it points to and pass it arguments just like 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 name or address. Function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.


Also visit my blog post [http://circuspartypanama.com how to hack clash of clans]
==Simple function pointers==
The simplest implementation of a function (or subroutine) pointer is as a [[variable (computer science)|variable]] containing the [[memory address|address]] of the function within executable memory. Older [[third-generation programming language|third-generation languages]] such as [[PL/I]] and [[COBOL]], as well as more modern languages such as [[Pascal (programming language)|Pascal]] and [[C (programming language)|C]] generally implement function pointers in this manner. Such pointers in older languages are generally less [[type safety|type-safe]] than in more modern languages, though, since the latter associate more [[data type|typing]] information with a function pointer variable, such as the data type of the [[return value]] of the function and the data type information of the [[parameter (computer programming)|parameter]]s to the function.<ref>{{cite web
| accessdate = 2011-04-13
| location = http://www.newty.de/
| publisher = logo
| title = The Function Pointer Tutorials
| quote = 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!
| url = http://www.newty.de/fpt/intro.html#top}}</ref>
 
=== Example in C ===
The following C program uses a function pointer to invoke one of two functions (<code>sin</code> or <code>cos</code>) indirectly from another function (<code>compute_sum</code>, computing an approximation of the function's [[Riemann integration]]). The program operates by having function <code>main</code> call function <code>compute_sum</code> twice, passing it a pointer to the library function <code>sin</code> the first time, and a pointer to function <code>cos</code> the second time. Function <code>compute_sum</code> in turn invokes one of the two functions indirectly by dereferencing its function pointer argument <code>funcp</code> multiple times, adding together the values that the invoked function returns and returning the resulting sum. The two sums are written to the standard output by <code>main</code>.
 
<source lang="c">
#include <math.h>
#include <stdio.h>
 
// Function taking a function pointer as an argument
double compute_sum(double (*funcp)(double), double lo, double hi)
{
    double  sum = 0.0;
 
    // Add values returned by the pointed-to function '*funcp'
    for (int i = 0;  i <= 100;  i++)
    {
        double  x, y;
 
        // Use the function pointer 'funcp' to invoke the function
        x = i/100.0 * (hi - lo) + lo;
        y = (*funcp)(x);
        sum += y;
    }
    return (sum/100.0);
}
 
int main(void)
{
    double  (*fp)(double);      // Function pointer
    double  sum;
 
    // Use 'sin()' as the pointed-to function
    fp = sin;
    sum = compute_sum(fp, 0.0, 1.0);
    printf("sum(sin): %f\n", sum);
 
    // Use 'cos()' as the pointed-to function
    fp = cos;
    sum = compute_sum(fp, 0.0, 1.0);
    printf("sum(cos): %f\n", sum);
    return 0;
}
</source>
 
==Functors==
{{main|Function objects}}
'''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 science)|closures]]. They are also used as callback functions if it is necessary to use a member function as a callback function.<ref>{{cite web
| accessdate = 2011-04-13
| date = 2005-01-31
| location = http://www.devx.com/
| 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 [[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 [[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. This makes the memory allocation independent from execution. The concept later was used by the multithreading processing ability.
 
==Method pointers==
C++ is [[object-oriented]], so classes can have [[method (computer science)|methods]]. Non-static member functions (instance methods) have an implicit parameter (the ''[[this (computer science)|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: <code>.*</code> or <code>->*</code> (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 <code>sizeof(Fx)==sizeof(void *)</code>, 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|date=August 2011}}.
 
== In C++ ==
A C++ typical use of "pointers to functions" is for passing a function as an argument to another function, since these cannot be passed dereferenced:
 
<source lang="cpp">
// Pointer to functions
 
#include <iostream>
 
using namespace std;
 
int add(int first, int second)
{
    return first + second;
}
 
int subtract(int first, int second)
{
    return first - second;
}
 
int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}
 
int main()
{
    int  a, b;
    int  (*plus)(int, int) = add;
    int (*minus)(int, int) = subtract;
 
    a = operation(7, 5, plus);
    b = operation(20, a, minus);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}
</source>
 
==See also==
* [[Delegation (programming)]]
* [[Function object]]
* [[Higher-order function]]
* [[Procedural parameter]]
 
==References==
{{Reflist|2}}
 
==External links==
* [http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.12 FAQ on Function Pointers], things to avoid with function pointers, some information on using [[function object]]s
* [http://www.newty.de/fpt/ Function Pointer Tutorials], a guide to C/C++ function pointers, [[callback (computer programming)|callbacks]], and [[function object]]s (functors)
* [http://www.codeproject.com/KB/cpp/FastDelegate.aspx Member Function Pointers and the Fastest Possible C++ Delegates], CodeProject article by Don Clugston
* [http://www.cplusplus.com/doc/tutorial/pointers.html Pointer Tutorials], C++ documentation and tutorials
* [http://www.codeproject.com/KB/security/Securefunctionpointer.aspx Secure Function Pointer and Callbacks in Windows Programming], CodeProject article by R. Selvam
* [http://publications.gbdirect.co.uk/c_book/chapter5/function_pointers.html The C Book], Function Pointers in C by "The C Book"
* [http://www.dbase.com/help/2_80/Language_Definition/IDH_LDEF_FUNCPOINTERS.htm Function Pointers in dBASE dBL], Function Pointer in dBASE dBL
 
[[Category:Data types]]
[[Category:Subroutines]]
 
[[de:Zeiger (Informatik)#Funktionszeiger (Methodenzeiger)]]

Revision as of 03:44, 8 December 2013

A function pointer (or subroutine pointer or procedure pointer) is a type of pointer supported by third-generation programming languages (such as PL/I, COBOL, Fortran,[1] dBASE dBL, and C) and object-oriented programming languages (such as C++ and D).[2] Instead of referring to data values, a function pointer points to executable code within memory. When dereferenced, a function pointer can be used to invoke the function it points to and pass it arguments just like 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 name or address. Function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.

Simple function pointers

The simplest implementation of a function (or subroutine) pointer is as a variable containing the address of the function within executable memory. Older third-generation languages such as PL/I and COBOL, as well as more modern languages such as Pascal and C generally implement function pointers in this manner. Such pointers in older languages are generally less type-safe than in more modern languages, though, since the latter associate more typing information with a function pointer variable, such as the data type of the return value of the function and the data type information of the parameters to the function.[3]

Example in C

The following C program uses a function pointer to invoke one of two functions (sin or cos) indirectly from another function (compute_sum, computing an approximation of the function's Riemann integration). The program operates by having function main call function compute_sum twice, passing it a pointer to the library function sin the first time, and a pointer to function cos the second time. Function compute_sum in turn invokes one of the two functions indirectly by dereferencing its function pointer argument funcp multiple times, adding together the values that the invoked function returns and returning the resulting sum. The two sums are written to the standard output by main.

#include <math.h>
#include <stdio.h>

// Function taking a function pointer as an argument
double compute_sum(double (*funcp)(double), double lo, double hi)
{
    double  sum = 0.0;

    // Add values returned by the pointed-to function '*funcp'
    for (int i = 0;  i <= 100;  i++)
    {
        double  x, y;

        // Use the function pointer 'funcp' to invoke the function
        x = i/100.0 * (hi - lo) + lo;
        y = (*funcp)(x);
        sum += y;
    }
    return (sum/100.0);
}

int main(void)
{
    double  (*fp)(double);      // Function pointer
    double  sum;

    // Use 'sin()' as the pointed-to function
    fp = sin;
    sum = compute_sum(fp, 0.0, 1.0);
    printf("sum(sin): %f\n", sum);

    // Use 'cos()' as the pointed-to function
    fp = cos;
    sum = compute_sum(fp, 0.0, 1.0);
    printf("sum(cos): %f\n", sum);
    return 0;
}

Functors

Mining Engineer (Excluding Oil ) Truman from Alma, loves to spend time knotting, largest property developers in singapore developers in singapore and stamp collecting. Recently had a family visit to Urnes Stave Church. 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.[4]

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. CLI 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. This makes the memory allocation independent from execution. The concept later was used by the multithreading processing ability.

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 inheritancePotter or Ceramic Artist Truman Bedell from Rexton, has interests which include ceramics, best property developers in singapore developers in singapore and scrabble. Was especially enthused after visiting Alejandro de Humboldt National Park..

In C++

A C++ typical use of "pointers to functions" is for passing a function as an argument to another function, since these cannot be passed dereferenced:

// Pointer to functions

#include <iostream>

using namespace std;

int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}

int main()
{
    int  a, b;
    int  (*plus)(int, int) = add;
    int (*minus)(int, int) = subtract;

    a = operation(7, 5, plus);
    b = operation(20, a, minus);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}

See also

References

43 year old Petroleum Engineer Harry from Deep River, usually spends time with hobbies and interests like renting movies, property developers in singapore new condominium and vehicle racing. Constantly enjoys going to destinations like Camino Real de Tierra Adentro.

External links

de:Zeiger (Informatik)#Funktionszeiger (Methodenzeiger)