function pointer(函数指针)

来源:互联网 发布:淘宝关于退货运费规则 编辑:程序博客网 时间:2024/05/29 14:37

function pointer (or subroutine pointer or procedure pointer) is a type of pointer supported by third-generation programming languages (such as PL/ICOBOL,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.

也就是函数指针是指向内存中(memory)的可执行的代码的一种特殊的指针。 函数指针可以作为另一个函数的参数, 从而间接的调用函数指针指向的函数。 

Method pointers[edit]

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.

C++的类中的 非静态的成员函数隐式的具有一个this pointer 指向成员函数作用的object。 所以有class 声明的对象也可以归为函数指针。 然后我们可以使用了this pointer 指向类的method 作用于自己对象本身。  为了处理虚继承, 我们的函数的成员指针通常实现为 “fat pointer”。 

举个例子:

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 function#include <iostream>using namespace std;int add(int first, int second) {    return first + second;}int substract(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 = 0;    int b = 0;    int (*Plus)(int, int) = add;    int (*Minus)(int, int) = substract;    a = operation(7, 5, Plus);    b = operation(20, a, Minus);    cout << "a = " << a << " b = " << b << endl;    return 0;}

运行结果如下:



However, in modern C++, it's generally preferred (for safety and convenience reasons) to use the C++ standard library class template std::function, of which the instances are function objects:

也就是现代的C++中, 使用函数指针的方式常常是使用C++的类模板库 function, 声明出来的对象就是函数。 如下求函数的导数:

#include <iostream>#include <functional>static double derivative(const std::function<double(double)> &f, double x0, double eps){    double eps2 = eps / 2;    double lo = x0 - eps2;    double hi = x0 + eps2;    return (f(hi) - f(lo)) / eps;}static double f(double x){    return x * x;}int main(){    double x = 1;    std::cout << "d/dx(x ^ 2) [@ x = " << x << "] = " << derivative(f, x, 1e-5) << std::endl;    return 0;}

运行结果如下:



0 0
原创粉丝点击