C + + Learning: function pointers

Source: Internet
Author: User

Previously in the book to see the function pointers related to not much attention, but recently in the actual work has come in handy. So I studied it carefully again.

Declaration of function pointers

It is very simple to declare a function pointer by replacing the function name in the function declaration with a pointer:

C + + int  Test (int  para1, double  *para2); //function declaration  int  (*PF) (int  para1, Span class= "Hljs-keyword" style= "Color:rgb (249,38,114)" >double *para2); //function pointer declaration  

Note: You must enclose the parentheses in the declaration *pf , because the parentheses have a higher precedence than the * operator, so:

C + + int  (*PF) (int  para1, double  *para2); //A pointer to a function  int  *pf (int  para1, double  *para2); //a function that returns a pointer  

The next step in declaring a function pointer is to assign a value to a function pointer, which is to point a function pointer to a (which is consistent with the basic type of pointer), The function name of a function is the address of the function :

c/c++PF = test; int  (*PF1) (int  para1, double  *para2) = test; //can also complete initialization of   
When the function pointer is declared

C++11 has the automatic type inference feature, which is a lot simpler:

auto pf = test;
Calling functions using function pointers

(*pf)The role is the same as the function name, so when you use (*pf) it, you only need to consider it as a functional name:

C + +int n = 0;double d = 0.0;int r = 0;r = (*pf)(n, &d);r = pf(n, &d); // 这种方式C/C++也是允许的
Array of function pointers

We may also need to use an array of function pointers, as shown in the following example:

C + +intTest1 (intPARA1,Double*PARA2);//Function declarationintTest2 (intPARA1,Double*PARA2);//Function declarationintTest3 (intPARA1,Double*PARA2);//Function declarationint(*pfarray[3])(intPARA1,Double*PARA2) = {test1, test2, test3};//function pointer array declaration and initializationr = *pfarray[0] (n, &d);//Function call

You can see that using a function pointer in this way is cumbersome, just imagine if the above function return value is a const pointer, and we want to declare the function pointer array as immutable, then const where should this be added? Here's another simple workaround, which is to simplify using typedef as we'll talk about it.

using typedef for simplification
C + + typedef  int  (*p_fun) (int  para1, double  *para2); //so that we can use a function pointer like a normal type  p_fun PF = Test;p_fun Pfarray[3 ] = {test1, test2, test3}; 

It ' s that easy!

Reference book: "C + + Primer Plus (6th edition) Chinese version"

C + + Learning: function pointers

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.