In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 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 deeply analyze the types of C++ in C++. 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.
In C++, C++ class template cannot be used directly. It must be instantiated into the corresponding template class, and the object of the template class is defined before it can be used. You can create an instance of the class template in the following ways.
Templates (template) use a completely general method to design functions or classes without having to specify in advance the type of each object to be used. The template function can be used to construct a series of related functions or classes. So templates can also be called parameterized types. In C++ type, templates can be divided into class templates (class template) and function templates (function template).
After explaining a function template in the program, when the compiler system finds that there is a corresponding function call, it will confirm whether it matches the corresponding parameter in the function template according to the type in the argument. Then generate an overloaded function. The definition body of the overloaded function is the same as that of the function template, which is called the template function (template function).
The difference between function template and template function is that function template is the definition of template, and general type parameters are used in the definition. The template function is a real function definition, which is generated by the compiler system when it encounters a specific function call, and has program code.
Similarly, after illustrating a class template, you can create an instance of the class template, that is, generate a template class. The difference between a class template and a template class is that a class template is the definition of a template. It is not a real class, and common type parameters are used in the definition; a template class is a real class definition and an instance of a class template.
1. Function template
Through the study of the previous knowledge, we can know that in the defined function, the type of the function parameter is fixed, and when the function is called, the type of the argument should be consistent with the parameter type of the tuned function, otherwise there will be an error of type inconsistency. Therefore, in the case of the same function but different types of parameters, it is obviously inflexible that different functions must be defined to complete the corresponding functions.
The function template function provided in C++ language is proposed to solve the above problems. The function template provided by C++ type can define a function that can operate on any type of variable, which greatly enhances the versatility of function design.
Because ordinary functions can only pass variable parameters, but the function template provides a mechanism for passing types in the above definition form, there can be one or more parameters, each parameter must be preceded by the class keyword to indicate the transfer type, and when there are multiple parameters, the parameters are separated by commas.
It can be seen that each parameter in the table represents a data type. The type of at least one parameter in the formal parameter table must be defined by the parameter in. The function template is just a description and cannot be executed directly. It needs to be instantiated as a template function before it can be executed. When the compilation system finds a function call: function name (argument table)
An overloaded function, the template function, is generated based on the type of argument in the argument Table and the defined function template. The definition body of the template function is the same as that of the function template, while the types in the formal parameter table are based on the actual types in the argument table.
two。 Class template
Can include one or more parameters, which can be either "type parameters" or "expression parameters". Each type parameter must be preceded by the class keyword to indicate that the class template represents a certain data type when instantiated.
That is to say, C++ type parameters are used to pass data types when the class template is instantiated; the type of expression parameters is a specific data type, and when the class template is instantiated, these parameters are provided with specific data, that is, expression parameters are used to pass specific data. When there are multiple parameters, they need to be separated by commas. Such as:
Template class myclass {/ / the definition body of the class}; this is how the editor shares how to deeply analyze the C++ types in C++. If you happen to have similar doubts, please 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.