C ++ function pointer usage

Source: Internet
Author: User

Maybe we seldom use function pointers in school learning. The usage may be used in the place where the callback function is passed by the API function. I am not very familiar with it.

1. Definition:

Pointer to a function type (the function type is determined by the return value and the form parameter)

2. format:

Function return type (* function pointer name) (parameter list );

For example:

 
INT (* Pmax) (int A, int B );

 

3. assignment:
1> direct assignment:

 
Int max (int A, int B); int (* Pmax) (int A, int B); Pmax = max;

 

2> Value assignment:

 
Int max (int A, int B); int (* Pmax) (int A, int B); Pmax = & Max;

 

4. Call

1> direct call

 
Int max (int A, int B); int (* Pmax) (int A, int B); void main () {Pmax = & Max; int c = Pmax (5, 4); cout <C <Endl ;}

2> use the unapply OPERATOR: (The unreference operator and the function pointer name must be framed in parentheses)

 
Int max (int A, int B); int (* Pmax) (int A, int B); void main () {Pmax = & Max; int c = (* Pmax) (5, 4); // note that cout <C <Endl;} must be enclosed in brackets between the quote operator and the function pointer name ;}

 

5. The function pointer is used as the form parameter:

Format:

 Return Value Function Name (parameter 1, parameter 2,..., function return value (* function pointer name) (parameter list ));

 
Bool changif (Int & A, Int & B, INT (* Pmax) (int A, int B ));

 

6. Return function pointer:

It is estimated that the function declaration with the return value as the function pointer will be depressing. However, if we have the skills and are careful enough, understanding is not a problem.
The function declaration for writing or reading the returned function pointer should be from the inside out

For example, write a function whose return value type is Max function pointer.
1> write the function name and function parameter table

 
Test ()

2> Add * Before the function name and enclose the prepared content with brackets (the brackets are required here)

 
(* Test ())

3> write the function pointer return type before the brackets, and write the function pointer parameter table behind the brackets.

 
INT (* test () (int A, int B );

 

Note:

1> Use typedef to first define the function pointer or function alias, and then use this name as a parameter or return value, or define the pointer variable to make it easier to understand and improveProgramReadability
2> Use typedef to define the function alias. When writing the function definition, this name can be used directly for function parameters, but this name cannot be used directly for function return values.

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.