For the so-called C ++ function pointer, the function pointer is a pointer to a function. The function pointer has two purposes: calling a function and making a function parameter, the following describes how to explain C ++ function pointers in detail.
The data type is identified by the first byte buffer [0]. There are 25628 possibilities. My task must process each possible data type, and my module contains several functions, which must be processed similarly in each function. Write the following code as follows:
- void MyFuntion( char* buffer, int length )
- {
- __int8 nStreamType = buffer[0];
-
- switch( nStreamType )
- {
- case 0:
-
- function1();
-
- break;
-
- case 1:
-
- ......
-
- case 255:
-
- function255();
-
- break;
-
- }
-
- }
If you write this method, you must make so many judgments in each of my functions. The code to be written must be very long, and each processing is performed, the correct processing function must be found after many judgments, and the code execution efficiency is not high. To solve the above problem, I thought of using the C ++ function pointer array to solve this problem.
The concept of function pointer is mentioned in the classic tutorial of C ++ programming by Mr. Tan haoqiang. In most cases, we cannot use it and ignore its existence. The function name is actually a pointer pointing to the function's entry address, but it is different from common pointers such as int * and double, take the following example to understand the concept of C ++ function pointers:
- 1 int funtion( int x, int y );
- 2 void main ( void )
- {
- 3 int (*fun) ( int x, int y );
- 4 int a = 10, b = 20;
- 5 function( a, b );
- 6 fun = function;
- 7 *fun)( a, b );
- 8 ……
- }
Statement 1 defines a function. Its input is two integer values, and the return value is also an integer. The input parameter and return value can be of any other data type.) Statement 3 defines a function pointer, different from the int * or double * pointer definition, the C ++ function pointer must specify the input parameter at the same time, indicating that this is a function pointer, and * fun must also be enclosed in a pair of parentheses;
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design