Function pointers, function pointers, and pointer Functions

Source: Internet
Author: User

Function pointers, function pointers, and pointer Functions

1. Use of function pointers

Int fun (int a, int B); // declare a function int (* p) (int, int) // define a function pointer, p is a function pointer, the int at the beginning refers to the return value type of the function, followed by the return value type of the function in brackets // assign a value to the function pointer p = fun; // p points to the function p = & fun; // and p = fun are equivalent. // Several equivalent forms of function call int a = p (1, 2 ); int B = (* p) (1, 2); int c = fun (1, 2 );

2. Several Forms of function pointer Definition

Defines the function pointer pointing to int fun (int a, int B ).

1) direct definition:

Int (* p) (int, int );

P = fun; // or p = & fun

2) use typedef to define the type alias:

// P1, p2 is a function pointer type

Typedef int (* p1) (int, int );

Typedef decltype (fun) * p2;

P1 f1 = fun; // here p1 is a function pointer type. You need to define a specific function pointer object f, and f is a pointer to the function.

P2 f2 = fun; // same as above

// Here, p1 and p2 are function types,

Typedef int p1 (int, int );

Typedef decltype (fun) p2;

P1 * f1 = fun; // f1 is a function pointer

P2 * f2 = fun; // f2 is a function pointer

3) use using to define the type alias

Using p1 = int (*) (int, int); // p1 is a function pointer type.

Using p2 = int (int, int); // p2 is a function type.

P1 f1 = fun; // f1 is a function pointer

P2 * f2 = fun; // f2 is a function pointer

3. function pointer Parameters

1) The function name is used as the form parameter, and the function name is automatically converted to the function pointer.

# Include <iostream> using namespace std; int sum (int a, int B) {return a + B;} void fun (int (* p) (int, int )) {cout <p (2, 3) <endl;} int main (int argc, char * argv []) {fun (sum ); // sum is automatically converted to function pointer return 1 ;}

 

2) define the function type alias

# Include <iostream> using namespace std; int sum (int a, int B) {return a + B;} typedef int f (int, int); void fun (f p) // f is the function type, and the form parameter is automatically converted to the function pointer type {cout <p (2, 3) <endl;} int main (int argc, char * argv []) {fun (sum); // here sum is converted to function pointer return 1 ;}

 

3) define a function pointer alias

# Include <iostream> using namespace std; int sum (int a, int B) {return a + B;} typedef int (* f) (int, int ); void fun (f p) // f is the function pointer type, and p is the function pointer {cout <p (2, 3) <endl;} int main (int argc, char * argv []) {fun (sum); // sum is automatically converted to function pointer return 1 ;}

4. returns the pointer to the function.

1) define directly

Int (* f (int) (int, int) // f (int) Is the function name and parameter list, and the return value type is int (*) (int, int)

2) Adopt the tail Return Method

auto f(int)->int(*)(int,int)                

3) Use a type alias to define the function pointer type

Tpyedef int (* p1) (int, int); using p2 = int (*) (int, int); typedef int p3 (int, int ); using p4 = int (int, int); // equivalent declaration p1 f (int); p2 f (int); p3 * f (int); p4 * f (int );

 

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.