Recognition: pointer to function

Source: Internet
Author: User
Abstract:
This article describes the usage of pointers to functions in detail.
---------------------------------------------------------
Statement:
This article is original. You are welcome to reprint it. Please keep the following information for reprinting.
Author: Nie Fei (afreez) Beijing-Zhongguancun
Contact: afreez@sina.com (welcome to contact the author)
Initial Release Date:
Without my consent, I shall not use words for commercial or profit purposes. Otherwise, the author has the right to pursue relevant responsibilities!

---------------------------------------------------------
I read the source code of directfb for another two days, and the header is big! But I don't admit it. She has a very personal structure and I also appreciate it. Today, when I read the function bool gacquire (cardstate * state, dfbaccelerationmask accel) {...} In/src/GFX/generic. C, I found a definition:
Genefxfunc * funcs;
Funcs points to a genefxfunc funcs [32];
Most of the subsequent operations on funcs will be:
...
* Funcs ++ = dacc_premultiply;
* Funcs ++ = dacc_xor;
...
Genefxfunc defines the format:
Typedef void (* genefxfunc) (genefxstate * gfxs );
I have never visited any country before. Later I found an article on the Internet and posted it as follows:
Author: csumck Source: csdn

Function pointer and typedef

Usage of function pointers in C ++ (including the usage of typedef)
(1) simple function pointer application.
// Form 1: return type (* function name) (parameter table)
Char (* pfun) (INT );
Char glfun (int A) {return ;}
Void main ()
{
Pfun = glfun;
(* Pfun) (2 );
}

The first row defines a pointer variable pfun. First, according to the "Form 1" mentioned above, we realize that it is a pointer to a function. This function parameter is of the int type, and the return value is of the char type. We cannot use this pointer only in the first sentence, because we have not assigned a value to it.
The second row defines a function glfun (). This function is a function that returns char with int as the parameter. We need to understand the function at the pointer level-the function name is actually a pointer, and the function name points to the first address of the function code in the memory.
Then there is the cute main () function. You should have understood its first sentence-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 pointed to by pfun. Of course, the content of the function glfun () is taken out, and the given parameter is 2.
(2) Use typedefIt is more intuitive and 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 typedef is to define a new type. The first sentence defines a ptrfun type and defines this type as a pointer to a function. This function takes an int as a parameter and returns the char type. You can use ptrfun like Int or char.
The code in the second line defines the variable pfun using this new type. In this case, the variable can be used as in Form 1.
(3) In C ++Class.
// Form 3: typedef return type (Class Name: * New Type) (parameter table)
Class ca
{
Public:
Char lcfun (int A) {return ;}
};
CA;
Typedef char (CA: * ptrfun) (INT );
Ptrfun pfun;
Void main ()
{
Pfun = Ca: lcfun;
CA. (* pfun) (2 );
}

Here, the definition and use of pointers are both subject to "class restrictions" or "objects ", it is used to indicate that the function pointing to the pointer is the class object of that class, which can also be obtained by using new. For example:
Ca * PCA = new CA;
PCA-> (* pfun) (2 );
Delete PCA;

In addition, this class Object Pointer can be a member variable within the class, and you can even use this pointer. For example:
Class CA has a member variable ptrfun m_pfun;
Void CA: lcfun2 ()
{
(This-> * m_pfun) (2 );
}

In a word, the class member function pointer must have a "-> *" or ". *" call.

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.