function pointer Small note

Source: Internet
Author: User

As with general data, functions also occupy a certain amount of space in memory, so functions also have the concept of "address", which refers to the "address" (that is, the starting address of the function code block), and the type of the function pointer is the function itself. Knowing the address, we can easily rely on the function pointer to complete the function call.

1. Simple function Pointers:

1 void Add (intint  b) {//write a very simple function here 2     cout << A + b <<  Endl; 3 }
1 void (*P1)  (intint  b);            Declares a function pointer to a function with the type "return void, two int parameter" of function 2 p1 = add; Assignment 3 //void (*P1) (intint b) = add;    You can also assign a value, a meaning of 4 (*P1) (12); Here are two methods of invocation, which are equivalent for a historical reason = =5 p1 (12);

2. An array containing multiple function pointers:

1 void (*p2[2])  (intint  b);            Declaration 2 p2[1] = add;            Assignment 3 p2[1] (2,3); Call, the same two methods can be 4 (*p2[1]) (3,4);

3. Pointer to an array containing multiple function pointers:
A little bit of a mouthful, this pointer points to the previous point in the "array containing multiple function pointers".

 1  void  (* (*P3) [2 ]) (int  A, int   b);              Nothing is to replace the P2 with *p3  2  p3 = &P2; The pointer must be initialized before the  3  (*P3) [1 ] = add;  4  (*P3) [1 ] (          1 , ); Call  5  ((*P3) [1 ]) (1 , 2 ); 

C++11 can be very simple to define directly: Auto P3 = &p2; Replace void (* (*P3) [2]) (int a, int b) = &p2;

function pointer Small note

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.