C & amp; C ++ function pointer

Source: Internet
Author: User

C & C ++ function pointer

Today, when I read the libcurl source code, I found that the method for defining the function pointer inside is different from the method I used at ordinary times. I analyzed it in detail.

The libcurl Code defines a set of function pointers for sending data. As follows:

 

// Code Directory: lib/urldata. hstruct connectdata {... Curl_send * send [2]; ......};
The definition of Curl_send is as follows:
// Code Directory: lib/urldata. h/* return the count of bytes sent, or-1 on error */typedef ssize_t (Curl_send) (struct connectdata * conn,/* connection data */int sockindex, /* socketindex */const void * buf,/* data to write */size_t len,/* max amount to write */CURLcode * err);/* error to return */

I am confused that if we usually use typedef to define function pointers, they are generally written in the following format:
// Define a function pointer. the pointer to the function receives an integer parameter and returns an integer value. Typedef int (* pFunc) (int );
However, the curl_send definition does not contain a pointer. After reading some documents, we found that such a definition method is also possible.

 

So I wrote the following program to verify it.

 

# Ifdef _ cplusplus # include
 
  
# Else # include
  
   
# Endifint testFunc (int para) {# ifdef _ cplusplusstd: cout <C ++ parameter is: <para <std: endl; # elseprintf (C parameter is: % d, para); # endifreturn 0;} int main () {typedef int (pTestFunc) (int); pTestFunc * pFunc = testFunc; // Method 1: OK · pFunc (1111); // Method 2: OK (* pFunc) (2222); // method 3: okpTestFunc * pFunc2 = & testFunc; // Method 4: okpFunc2 (3333); return 0 ;}
  
 
If you save the above program as a C program file (. c) and compile it, the following running result is obtained:
C parameter is: 1111
C parameter is: 2222
C parameter is: 3333

If the file is saved as a C ++ program file (. cpp), run the following command:
C ++ parameter is: 1111
C ++ parameter is: 2222
C ++ parameter is: 3333

The above results show that:
1. It is also possible to use the same function declaration method as curl_send. The above pTestFunc definition.
2. For the initialization of the function pointer pFunc, either method is acceptable. Refer to the methods 1 and 4 above.
3. For function pointer pFunc calls, you can also use different methods. For more information, see Methods 2 and 3.
4. For C and C ++, these definitions and function pointers are supported.

Of course, we can also use the traditional function pointer declaration method, as shown in the following program:

 

Int main () {typedef int (* pTestFunc) (int); pTestFunc pFunc = testFunc; // Method 5: okpFunc (1111); // Method 6: OK (* pFunc) (2222); // Method 7: okpTestFunc pFunc2 = & testFunc; // Method 8: okpFunc2 (3333); return 0 ;}
The running result is exactly the same as described above.

 

 

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.