Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the uses of C++ function pointers

Shulou Source: shulou.com Published: 2022-06-02 03:51:52 09月22日 Update

The knowledge points of this article "what are the uses of C++ function pointers" are not quite understood by most people, so the editor summarizes the following, with detailed contents and clear steps, which can be used for reference. I hope you can get something after reading this article. Let's take a look at this "what are the uses of C++ function pointers?"

Detailed understanding of C++ function pointer A preliminary understanding of function pointer

A function pointer is a pointer to a function, which is not equal to a function. It can perform the operation of a function and can be understood together with pointer variables. Its basic declaration format is:

Type (* ptrname) (formal parameter list)

For example, declare a function pointer that compares the size of two strings

Bool (* cp) (const string S1, const string S2)

It is important to note that do not remove the parentheses! If you remove the parentheses, the meaning becomes a declaration of a function that returns a bool pointer!

Function pointer points to function type

When using function pointers, function pointers can perform the same function as functions, that is, they can also perform tasks, but function pointers need to be initialized, and there is no conversion of function pointer types. During initialization, function pointers can be assigned to nullptr or constant NULL, or point to a function, but strict requirements are required when pointing to this function. It needs to strictly follow the return types of the two, and the formal parameter list corresponds to each other.

Examples

/ / two sample functions bool compareLength (const string S1, const string S2) {/ / return true when the length of S1 is greater than the length of S2, otherwise return false return s1.size () > s2.size ()? true:false;} int getLength (const string S1) {/ / return the length of the string return s1.size ();} / / initialize the previously defined cp function pointer cp = nullptr; / / correct, initializing to null cp = NULL / / correct, initialized to empty constant NULLcp = compareLength; / / correct, function return form and formal parameter list and type are the same cp = getLength; / / error, return type is different from formal parameter list

Use function

Following the idea above, the way the function is called can be written as follows

/ / function pointer calls function cp (S1, S2); / / call 2 (* cp) (S1, S2); / / function call, same as cp compareLength (S1, S2); function pointer is used as formal parameter

You can pass in and use a function or a function pointer as a formal parameter of a function. For example, an instance of a function pointer is urgently needed in the constructor of the thread header file thread of Category header 11.

# includestd::thread t (function pointer,.. Args)

The declaration definition is in the following form, such as passing the function or function pointer defined above into a new function as the basis for comparison between the two.

Int packageFunc (const string & S1, const string & S2, bool comp (const string & S1, const string & S2)) {if (comp (S1, S2)) {cout

Tags: Function pointer type form formal parameter content centering pointing to face two equivalent length code example parameter handle character string that is constant Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Technology Xiaomi vpn NVidia