function pointers and callback functions

Source: Internet
Author: User

First, the function pointer

The target code of the function has an entry address, which corresponds to a pointer to the function. A pointer to a function can be assigned to a pointer variable that points to a particular function class, and the function it points to can be called by a pointer variable pointing to the function.

The general form of a pointer variable that defines a point to a function is

return value type (* pointer variable name) (parameter list)

For example:

Int (*p) ();//define P is a pointer variable to a function whose function return value is int.

When you define a pointer variable to a function, you can assign a function's entry address to it. The function name itself is the function entry address constant, and when a pointer to a function points to a function, it can be used to invoke the function it refers to. The general form of a function call is

Function name (argument table)

Use a pointer variable to a function to call the function, written as

(* pointer variable name) (argument table)

For example: the function min (A, a, b) for two integers returns the value of int, statement f=min; make pointer variable f point to Min function, statement

Z=min (x, y); can be written as: z= (*f) (x, y);

Second, callback function

A callback function is a function that is called through a function pointer. If you pass a pointer to a function as a parameter to another function, the callback function is called when the pointer is used to invoke the function it points to. A callback function can be called by a program just like a normal function, but only when it is passed as a parameter to the called function can it be called a callback function .

1. Why to use callback function

Because it is possible to separate the caller from the callee, the caller does not care who the callee is, and all it needs to know is that there is a called function with some particular prototype, certain constraints.

The following programs:

The maximum value in the array is obtained by returning the function.

#include <iostream>using namespace std;int max (int arr[], int n); void process (int*p, int n, int (*P1) (int arr[], int n ); void main () {int a[4] = {Ten, 8, 9};int n = 4;cout << "The max=";p rocess (A, n, Max);} int max (int arr[], int n) {int I;int max1 = arr[0];for (i = 0; i < n; i++) {if (Max1 < arr[i]) {max1 = Arr[i];}} return (MAX1);} void process (int*p, int n, int (*P1) (int arr[], int n)) {int result;result = (*P1) (P, n); cout << result << Endl; }


function pointers and callback functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.