In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.