Transfer function pointer in C ++

Source: Internet
Author: User

Function pointers are a very good type. Therefore, you can compile a function. One of its parameters is the function pointer. Then, when the (external) function uses its function pointer parameter, it indirectly calls the function that the corresponding parameter points to when calling the function.

Since pointers can point to different functions under different circumstances, the caller is allowed to determine which function to call from an external function.

When calling a function with a function pointer type parameter, the parameter can only contain the corresponding type pointer of the function address. You can also use the function name as a parameter to display the transfer function. A function that is transmitted as a parameter to another function is sometimes called a callback function.

Example:

# Include

 
  
Using std: cout; using std: endl; // function declaration double squared (double); double cubed (double); double sum_array (double array [], int len, double (* pfun) (double); int main () {double array [] = {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5 }; int len = sizeof array/sizeof array [0]; cout <"Sum of squares =" <

 

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.