In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to analyze the basic concepts of C++ imitation function. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The C++ programming language has brought us great benefits. However, even the functions we often use also have a lot of advanced content worthy of our in-depth discussion. Here we will introduce in detail the basic concepts of C++ imitating functions to facilitate your interpretation of the language.
The word C++ imitating function often appears in template libraries (such as STL), so what is imitated function?
As the name implies: an imitation function is something that can work like a function. Please forgive me for using such a pronoun. I will explain it slowly below.
Void dosome (int I)
This dosome is a function, and we can use it like this: dosome (5)
So, is there anything that can work like this?
Answer 1: objects with the () operator overloaded, such as:
Struct DoSome {void operator () (int I);} DoSome dosome
Here the class (for C++, struct is the same as the class) overloads the () operator, so its instance dosome can use dosome (5) like this; it's exactly the same as the function call above, isn't it? So dosome is a C++ imitating function.
In fact, there is the answer 2:
The object that the function pointer points to.
Typedef void (* DoSomePtr) (int); typedef void (DoSome) (int); DoSomePtr * ptr=&func; DoSome& dosome=*ptr; dosome (5); / / it's exactly the same as a function call again.
Of course, the answer 3 member function pointer to the member function is the expected answer.
The use of C++ imitating function
Whether they are objects, function pointers, etc., they can be passed as parameters or saved as variables. So we can pass a mock function to a function that calls the mock function as needed (kind of like a callback).
This technique is widely used in the STL template library to achieve the "flexibility" of the library. For example: for_each, its source code is roughly as follows:
Template
< typename Iterator, typename Functor >Void for_each (Iterator begin, Iterator end, Fucntor func) {for (; beginchild; begin++) func (* begin);}
This for loop iterates through every element in the container, calling the mock function func for each element, thus implementing the programming idea of "do the same thing for each element." In particular, if the imitating function is an object, the object can have member variables, which gives the C++ imitating function a "state", thus achieving higher flexibility.
The above is the editor for you to share how to analyze the basic concept of C++ imitation function, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.