The function pointers of C + + learning notes

Source: Internet
Author: User

Similar to data items, functions also have addresses. The address of the function is where the memory where the machine language code is stored begins.

First, the basic knowledge of function pointers

Suppose you want to design a function called estimate () that estimates the time it takes to write a specified number of lines of code, and you want the function to be used by different programmers, and this function allows each programmer to provide his own algorithm to estimate the time. To achieve this goal, the mechanism used is to pass the algorithm function address of the programmer to estimate () and must complete the following tasks:

    • Get function Address
    • Declaring a function pointer
    • To invoke a function with a function pointer

1. Get the function address

Use the function name (not followed by arguments). such as: Think () is a function, then think is the address of the function. To pass a function as an argument, you must pass the function name.

Be careful to distinguish between the address of a function or the return value of a function:

1 process (think);    // the address of the transfer function think () to process () 2 // The return value of the transfer function think () to though ()

2. Declaring function pointers

The declaration should specify the return type of the function pointed to by the pointer and the function's signature (parameter list), assuming that Pam has written a function to estimate the time, the prototype is as follows:

1 Double pam (int);    // function Declaration

The correct pointer type is declared as follows:

1 Double (*PF) (int2//  PF points to an input int parameter, returns a double worth the function

As you can see, the function name in the function declaration is replaced with *PF, so PF becomes a pointer to the function, the basic knowledge of the pointer can be known, (*PF) is also a function.

The *PF must be enclosed in parentheses in the declaration, otherwise the following:

1 double *PF (int);

This is equivalent to declaring a function PF whose return value is a pointer to a double, which is completely not matter with the function pointer we want to declare.

After the PF is correctly declared, the corresponding function address can be assigned to it:

1 Double pam (int); 2 Double (*PF) (int); 3 pf = pam;

Now the pointer pf is pointing to the function Pam (). again, the feature and return type of Pam () must be the same as PF.

Suppose you want to pass the address of the code line and the estimate algorithm (such as the PAM () function) to estimate (), the prototype is as follows:

1 void estimate (intdouble (*PF));

For estimate () to use the PAM () function, you need to pass the address of Pam () to it:

1 estimate (Pam);

3. Using pointers to invoke functions

As mentioned above, (*PF) plays the same role as the function name, so when you use (*PF), you just need to consider it as the name of the functor:

1 Double pam (int); 2 Double (*PF) (int); 3 pf = Pam; 4 double x = Pam (4);   // calling a function with a function name 5 double y = (*PF) (5);  // calling functions with the pointer pf

However, C + + also allows PF to be used as with the function name:

double y = pf (5);  // same as Double y = (*PF) (5) Effect

Why is PF equivalent to (*PF)? Because there are two views, one school of thought, because PF is a function pointer, and *PF is a function, you should use (*PF) () as a function call, and another school of thought, because the function name is a pointer to the function, the function pointer should be similar to the function name, so the PF () should be used as a function call. It seems reasonable to say that C + + has a tradeoff-two methods are correct, although logically conflicting.

Example of a function pointer

1 /*function Pointer Example*/2#include <iostream>3 4 using namespacestd;5 6 //declaration of two functions that will be called7 DoubleBetsyint);8 DoublePamint);9 Ten //estimate () statement One voidEstimateintLinesDouble(*PF) (int)); A  - intMain () - { the     intCode; -  -cout <<"Please enter the number of lines of code:"; -CIN >>Code; +cout <<"Betsy's evaluation results: \ n"; - estimate (code, Betsy); +cout <<"Pam's evaluation results: \ n"; A estimate (code, PAM); at     return 0; - } -  - DoubleBetsyintLNS) - { -     return 0.05*LNS; in } -  to DoublePamintLNS) + { -     return 0.03* LNS +0.0004* LNS *LNS; the } *  $ voidEstimateintLinesDouble(*PF) (int))Panax Notoginseng { -cout << Lines <<"Line code will take time"; thecout << (*PF) (lines) <<"hours \ n"; +}

Operation Result:

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.