Meanings of function pointers in C/C ++

Source: Internet
Author: User

Functions are stored in the code area of the memory. They also have addresses. How can we obtain the address of the function?

If we have an int test (int A) function, its address is the name of the function. Like an array, the array name is the starting address of the array.

Define a pointer to a function in the following form. The preceding test () is used as an example:

INT (* FP) (int A); // a pointer to the function is defined here.

Function pointers cannot point to different types or functions with different parameters. When defining function pointers, we can easily make the following mistakes.

Int * FP (int A); // here it is incorrect, because according to the combination and priority, it is first combined with (), and then becomes a function that returns an integer pointer, instead of function pointers, pay special attention to this!

Here is a specific example:

# Include <iostream>
# Include <string>
Using namespace STD;

Int test (int );

Void main (INT argc, char * argv [])
{
Cout <test <Endl; // display the function address
INT (* FP) (int );
Fp = test; // assign the address of the function test to the Function Learning pointer fp
Cout <FP (5) <"|" <(* FP) (10) <Endl;
// Output FP (5) above, which is written in the Standard C ++. (* FP) (10) This is a standard writing method compatible with C. The two methods agree, but note the difference, avoid writing programs with portability issues!
Cin. Get ();
}

Int test (int)
{
Return;
}

The typedef definition can simplify the definition of a function pointer, but it is not easy to define it. The above code is rewritten as follows:

# Include <iostream>
# Include <string>
Using namespace STD;

Int test (int );

Void main (INT argc, char * argv [])
{
Cout <test <Endl;
Typedef int (* FP) (int A); // Note: This is not a life function pointer, but a type of function pointer. This type is defined by yourself and its name is FP.
Fp fpi; // use the type name FP defined here to define a FPI function pointer!
FPI = test;
Cout <FPI (5) <"|" <(* FPI) (10) <Endl;
Cin. Get ();
}

Int test (int)
{
Return;
}

Function pointers can also be passed to functions as parameters. Let's take a look at the example below. Read it carefully and you will find its usefulness, A little reasoning makes it easy for us to do some complex programming work.

// ------------------- The above example is slightly modified as the basis -----------------------------
# Include <iostream>
# Include <string>
Using namespace STD;

Int test (INT );

Int Test2 (INT (* RA) (INT), INT );

Void main (INT argc, char * argv [])
{
Cout <test <Endl;
Typedef int (* FP) (INT );
Fp fpi;
FPI = test; // The memory address that FPI assigns to the test function

Cout <Test2 (FPI, 1) <Endl; // when the Test2 function is called here, the address of the function stored by FPI (the address of the test function) the first parameter passed to Test2
Cin. Get ();
}

Int test (int)
{
Return A-1;
}

Int Test2 (INT (* RA) (INT), int B) // a function pointer named Ra is defined here.
{
Int c = Ra (10) + B; // after the call, Ra has pointed to the function address pointed to by FPI, that is, the test function.
Return C;
}

Using the function pointer, we can construct a pointer array. The more explicit point statement is to form a pointer array pointing to the function, which may be easier to understand.

# Include <iostream>
# Include <string>
Using namespace STD;

Void T1 () {cout <"test1 ";}
Void T2 () {cout <"Test2 ";}
Void T3 () {cout <"test3 ";}
Void main (INT argc, char * argv [])
{
Void * A [] = {T1, T2, T3 };
Cout <"comparison T1 () whether the memory address of is consistent with the address stored in array a [0] "<t1 <" | "<A [0] <Endl;

Cout <A [0] (); // error! Pointer arrays cannot use array subscript operations to call functions.

Typedef void (* FP) (); // you can specify a function pointer type.
Fp B [] = {T1, T2, T3}; // use the custom Type FP to define B [] as a pointer array pointing to the Function
B [0] (); // now the subscript operation can be performed using the pointer array pointing to the function to indirectly call the function;
Cin. Get ();
}

Taking a closer look at the above example, you may know how it will take for a while. In the end, let's make a summary, just remember this, it is easy to understand how to use function pointers to construct Arrays for indirect function calls!

Void * A [] = {T1, T2, T3 };
Cout <"comparison T1 () whether the memory address of is consistent with the address stored in array a [0] "<t1 <" | "<A [0] <Endl;

Cout <A [0] (); // error! Pointer arrays cannot use array subscript operations to call functions.

Why can't I call this error line in the preceding section?

We have already made it clear in the previous tutorial, but here we will review the concept. All the elements in the pointer array save is a memory address, since it is only a memory address, it is impossible to carry out a [0] () such address with parentheses, and function pointers are different, it is an exception, the function pointer is called only because it is a pointer to the code area in the memory of the function. It is authorized by the system to operate in parentheses for indirect function calls, since the function pointer allows this operation, the array defined as the function pointer must be able to perform the same operation.

 

Use the function pointer in the 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.

Related Article

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.