The original version of C ++ Language evolved from a top-level version of C with Class. C ++ is an evolutionary version of C language. Today, we will introduce the basic concept of C ++ function pointer in detail.
In C/C ++, data pointers are the most direct and commonly used. Therefore, it is easier to understand them. Function pointers are a common and useful method for dynamic call during runtime, such as CallBack Function.
Let's briefly talk about the C ++ function pointer.
Regular function pointer
- void(*fp)();
Fp is a typical function pointer used to point to a function without parameters or return values.
- void(*fp2)(int);
Fp2 is also a C ++ function pointer, which is used to point to a function with an integer parameter and no return value.
Of course, experienced people generally suggest using typedef to define the type of function pointers, such:
- Typedef void (* FP )();
- FP fp3; // the same definition as the above fp.
The main reason why C ++ function pointers fear beginners is that they have too many parentheses. Some function pointers are often stuck in the brackets, I will not give an example here, because it is not the scope discussed in this article. The typedef method can effectively reduce the number of parentheses and clarify the layers, so it is recommended. This article only considers simple function pointers, so typedef is not used for the time being.
Assume there are two functions:
- void f1()
- {
- std::cout << "call f " << std::endl;
- }
- void f2(int a)
- {
- std::cout << "call f2( " << a << " )" << std::endl;
- }
Now we need to use the C ++ function pointer to call the function. We need to specify the function for the pointer:
- Fp = & f1; // you can also use: fp = f1;
- Fp2 = & f2; // you can also use: fp2 = f2;
- Void (* fp3) () = & f1; // you can also use: void (* fp3) () = f1;
- // The call time is as follows:
- Fp (); // or (* fp )();
- Fp2 (1); // or (* fp2) (1 );
- Fp3 (); // or (* fp3 )();
The two call methods have the same effect. I recommend the previous method. The latter is not only an extra keyboard, but also a loss of flexibility. It is not mentioned here.
C ++ emphasizes type security. That is to say, different types of variables cannot be directly assigned values. Otherwise, a warning is taken lightly and an error is returned when the values are duplicated. This is a very useful feature that can often help us find problems. Therefore, people of insight believe that any external warning in C ++ cannot be ignored. Some people even suggested that no warning information should appear during compilation, that is, the warning should be handled as an error.
For example, if we assign f1 to fp2, the C ++ Compiler (vc7.1) will report an error:
- Fp2 = & f1; // error C2440: "= ":
Cannot be converted from "void (_ cdecl *) (void)" to "void (_ cdecl *) (int )"
- Fp1 = & f1; // OK
In this way, the compiler can help us identify encoding errors, saving our troubleshooting time.
Consider the sort function of the C ++ Standard Template Library:
- // Quick sorting Function
- Template <typename RandomAccessIterator, typename BinaryPredicate>
- Void sort (
- RandomAccessIterator _ First, // The First element location of the data to be sorted
- RandomAccessIterator _ Last, // The Last element location of the data to be sorted is not sorted)
- BinaryPredicate _ Comp // comparison algorithm used for sorting (can be C ++ function pointers, function objects, etc)
- );
For example, we have an integer array:
- int n[5] = {3,2,1,8,9};
To sort it in ascending order, we need to define a comparison function:
- bool less(int a, int b)
- {
- return a < b;
- }
Then use:
- sort(n, n+5, less);
To sort it in descending order, we only need to change the comparison function. The standard templates of C/C ++ already provide the less and great functions. Therefore, we can use the following statements to compare them:
- sort(n, n+5, great);
In this way, the sort function can be sorted by any method without changing the definition of the sort function. Is it flexible?
This usage is very popular in the standard template library (STL) of C ++. In addition, the CallBack function is often used in the operating system. In fact, the so-called CallBack function is essentially a C ++ function pointer.
It looks simple. This is the most common usage of C language pointers. This was a wonderful thing, but when C ++ came, the world began to change.