C++ function Pointers are Shared using examples

  • 2020-04-02 02:16:15
  • OfStack

demand
Suppose you want to design a function called estimate () that estimates the time it takes to write the specified number of lines of code, and you want it to be available to different programmers.

Some of the code in the estimate () is the same for all users, but the function allows each programmer to provide his own algorithm to estimate the time.

To achieve this, the mechanism is to pass the address of the algorithm function to be used by the programmer to the estimate ().

The implementation code is as follows


//Funpointer. CPP: defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>double betsy(int);
double pam(int);
//The second argument to the estimate function takes a function pointer
void estimate(int lines,double (*pf)(int));
int _tmain(int argc, _TCHAR* argv[])
{
    using namespace std;
    int code;
    cout<<"How many lines of code do you need?"<<endl;
    cin>>code;
    cout<<"Here's Betsy's estimate:"<<endl;
    estimate(code,betsy);
    cout<<"Here's Pam's estimate:"<<endl;
    estimate(code,pam);
    getchar();
    getchar();
    return 0;
}
inline double betsy(int lines){return 0.05*lines;}
inline double pam(int lines){return 0.03*lines+0.004*lines*lines;}
inline void estimate(int lines,double (*pf)(int))
{
    using namespace std;
    cout<<lines<<" lines will take "<<(*pf)(lines)<<"hour(s)"<<endl;
}

The results

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201403/20140312161536.jpg? 2014212161640 ">

Benefits of using function Pointers
The above design is helpful for future program development. When a programmer develops his own algorithm for estimating time, he will not need to rewrite the estimate () function. Instead, he simply provides the address of his function and guarantees that the function's arguments and return types are correct.

The function pointer allows the programmer to modify the behavior of the estimate (), even though he has no access to the source code for the estimate ().

Inline function
Since the function implementation is simpler, you can use inline functions instead of regular functions.

To use inline functions (a new feature of C++ to speed up your program), you must take one of the following steps:

Add the keyword inline before the function declaration
Add the keyword inline before the function definition
Inner - linked functions run a little faster than regular functions, but at the cost of more memory. If the program calls the same inline function in 10 different places, the program will contain 10 copies of the function code, as shown in the following figure:

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201403/20140312161602.jpg? 2014212161713 ">

When should I consider using inline functions?
  The execution time of the function code is very short
If C macros perform function-like functions, consider converting them to C++ inline functions
  Note: when a function is too large or recursive, the compiler may not treat it as an inline function.


Related articles: