C + + function pointer use __ function

Source: Internet
Author: User
About the use of function pointers in C + + (including a discussion of typedef usage) (i) Application of simple function pointers. 
Form 1: Return type (* function name) (parameter table) char (*pfun) (int); 
Char glfun (int a) {return;} 
    void Main () {pfun = Glfun; (*pfun) 
(2); The first line defines a pointer variable pfun. First, we recognize that it is a pointer to a function, based on the previous "Form 1", which is an int and the return value is the char type. 
        Only the first sentence we have not yet been able to use this pointer because we have not yet assigned it. The second line defines a function glfun ().
        The function is exactly a function that returns char with the int as an argument. 
        We want to understand the function at the level of the pointer--the function name is actually a pointer to the function's code in the first address in memory.
        And then there's the cute main () function, and the first sentence you should read-it assigns the address of the function Glfun to the variable pfun. 

In the second sentence of the main () function, "*pfun" is obviously the content of the address that Pfun is pointing to, and of course the contents of the function Glfun () are taken out, and the given argument is 2. 
(ii) The use of TypeDef is more intuitive and more convenient. 
Form 2:typedef return type (* new type) (parameter table) typedef char (*PTRFUN) (int); 
Ptrfun Pfun; 
Char glfun (int a) {return;} 
    void Main () {pfun = Glfun; (*pfun) 
(2); The function of a typedef is to define a new type.
        The first sentence defines a ptrfun type and defines the type as a pointer to a function that takes an int as its parameter and returns the Char type. 
        You can use Ptrfun just as you would with Int,char.

The second line of code uses this new type to define the variable pfun, and you can use this variable as you would use Form 1. 
(iii) Use function pointers in C + + classes. 
Form 3:typedef return type (class name::* new Type) (parameter table) class CA {public:    Char lcfun (int a) {return;} 
}; 
CA CA; 
typedef char (CA::* ptrfun) (int); 
Ptrfun Pfun; 
    void Main () {pfun = Ca::lcfun; 
ca. (*pfun) (2); In this case, the definition and use of the pointer is combined with a "class limit" or "object", which indicates that the function that the pointer is pointing to is the class object here and can be obtained using new. 
For example: CA *PCA = new CA; 
Pca-> (*pfun) (2); 
        Delete PCA; And this class object pointer can be a class internal member variable, and you can even use the this pointer. 
For example: Class CA has member variable ptrfun m_pfun; 
void Ca::lcfun2 () {(This->*m_pfun) (2); In a word, using a class member function pointer must have a call of "->*" or ". *".

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.