In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "what are the function concepts in C++". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this "what are the function concepts in C++" article can help you solve the problem.
(1) rules for the use of functions
The definition of a function cannot be nested, but calls can be nested.
When a function is called, if a default parameter is to specify a specific value, all parameters before it must be assigned.
When assigning default real parameters, once a parameter is assigned a default value, all parameters behind it must have a default value, because the order of setting the default parameters is from right to left; and note that the default value cannot be a local variable
The default value of a function parameter can be an expression
If the default parameter is set when the function is defined, it cannot be set again when the function is declared, and vice versa
Function has only one return value, except for a function of type void
A function call can appear in an execution statement, in an expression, or even as an argument to a function, but not as a formal parameter of a function, because the return value of the function is in the register and has no address and cannot be used as a formal parameter.
Function is a special data type, correct
When the function is not of type void and there is no return statement in the function, the return value of the function is the same as the return type, but the content is random.
All functions in C++ are essentially external functions (which can be extended to other files), so the extern keyword can be omitted.
If the formal parameter of a function is a pointer variable to an ordinary variable, the argument can only use a pointer to an ordinary variable, not a pointer to a const variable, or vice versa
When the number of function independent variables is uncertain, the system does not automatically detect independent variables.
13. There are three kinds of parameters for function:
① value transfer: the memory space will be reallocated for the parameter, and the value of the parameter will be copied to the parameter. The change of the parameter will not affect the value of the parameter. After the function is called, the parameter will be released.
Pass the ② address: the parameter is a pointer variable. Pass the address of the argument to the function, and you can change the value of the argument in the function. Memory is allocated to the parameter pointer variable when called, and the pointer variable is released at the end.
③ reference pass: no memory space will be reallocated for the parameter. The parameter is just an alias for the argument. The change of the parameter will only affect the value of the parameter. After the function call ends, the parameter will not be released.
(2) the use of functions
To establish a custom function, you only need to understand the function of the function when calling, so the readability of the program is improved.
The value returned by sizeof has the following meaning (in bytes):
Array-the amount of array space allocated at compile time
Pointer-the amount of space used to store the pointer (the length of the address where the pointer is stored, which is a long integer, should be 4)
Type-the amount of space occupied by this type
Object-the actual amount of space occupied by the object
Function-the amount of space occupied by the return type of the function. The return type of a function cannot be void
Sizeof (float) is an (integer) type expression
Math.floor () indicates rounding down and returns the double type
Math.ceil () indicates rounding up and returns the double type
Math.round () is rounded to return the int type
The user can overload (cannot redefine) the standard library function, if so, the function will lose its original meaning, but if the standard library header file and related namespace are already included, the system does not allow the user to redefine the standard library function. Because if the function elements of two functions in the same scope are the same except for the return value type, the compiler will report a redefinition error
The function returns the value as the right value, which is not modified by const, so const is equivalent to no modification at this time.
If the parameter type is inconsistent, the argument is converted according to the parameter type implicitly when the function is called.
The main function returns a value of type int by default
If you pass a string to a function with an address, the function parameter can be either a character array or a pointer variable.
The return value of a function can be a reference type and a function return reference can be used as a left value
The return type of a function can be a structure type, in which case the function returns a structure object
All functions are visible in the program that defines them.
(3) preprocessing commands (including macro definitions)
The preprocessing command line cannot end with a semicolon
The preprocessing command line can appear on the last line of the program
The scope of the preprocessing command line is from the location of the occurrence to the end of the source program file
All lines that begin with the # sign are not necessarily preprocessed command lines for compilation
You cannot have more than one preprocessing command on one line of the source file
Preprocessing does not do grammar checking
C++ pre-processing commands are processed by the preprocessor before compilation (so they are executed before compilation), and syntax analysis is performed at compile time.
Macro replacement does not take up the running time of the program, but only takes up the compilation time.
(4) the difference between inline functions and macros
Inline functions can be debugged at run time, while macro definitions cannot
The compiler does safety checks or automatic type conversions on the parameter types of inline functions (like ordinary functions), while macro definitions do not
Inline functions can access member variables of a class, while macro definitions cannot
Declare a member function that is also defined in the class and automatically convert it to an inline function.
(5) the difference between functions and macros
Macros do simple string substitution (note string substitution, not other type parameter substitution), while function parameters are passed with data types, which can be of various types.
The parameter substitution of a macro is handled directly without calculation, while the function call passes the value of the argument to the parameter, which is naturally calculated.
Macros are done before compilation, that is, the macro name is replaced with the macro body, and then compiled, and the function is obviously called at execution time after compilation. Therefore, macros take up compilation time, while functions take up execution time.
The parameters of macros do not occupy memory space, because they only replace strings, and the parameter transfer during function calls is the transmission of information between specific variables. as local variables of the function, formal parameters obviously occupy memory.
The function call needs to pay a certain cost of time and space, because when the system calls the function, it has to keep the scene, then transfer to the called function to execute, call it, and then return to the main tone function, and then resume the scene at this time, these operations are obviously not in the macro.
Macro replacement does not take up the running time of the program.
Macros have nothing to do with types, but functions in C++ must specify a return type, so macros can do what functions cannot do.
(6) function template
1. The function template is in the following format:
Template returns type function name (parameter list) {function body}
Where class can be replaced by the typename keyword
There is no need to explicitly specify the type when the function template is called, and the system automatically matches the parameter type. If there is no suitable one, an error will be reported. On the other hand, the type needs to be explicitly specified when using the class template, and for the function template, it is important to return the same type of value and parameter.
When both the template function and the ordinary function meet the conditions, the ordinary function is executed first.
Template specialization: (when the function template needs to specialize some types, it is called the specialization of the function template, and the specialization of the class template)
① because many times, we need a template to deal with a variety of situations, but also need it to have a special treatment for a particular type, so there is template specialization.
On the whole, ① specialization can be divided into full specialization and partial specialization.
② full specialization: that is, all template parameters in the template are specified as determined types. Full specialization means that a new type is defined, and the functions in the fully specialized class can be different from the template class.
③ specialization: the template parameters in the template have not been fully determined and need to be determined by the compiler at compile time
The priority of ④ for calling the template class, full specialization class and bias class of the main version is as follows: full specialization class > partial specialization class > main version template class.
⑤ when a function call finds a specialized matching function, it will first call the specialized function instead of instantiating it through the function template.
⑥ template specialization is equivalent to declaring that template specialization is required after declaring a class template, etc., and then let the following code use a type, so it cannot be used alone, for example;! [img] (file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml\wps386C.tmp.jpg)
5. Template specialization implementation: template + class implementations or function definitions that have nothing to do with the template type
① is specialized to absolute type (full specialization): for example: templateclass Compare {...}; / / specialized to float type, which is function template specialization; function template can only be fully specialized, not biased
② specializes to reference, pointer type (semi-specialized, partial): example: template struct iterator_traits {}
③ is specialized to another class template (specialization): example: template class Compare {}
The member functions of class templates are all function templates; unused member functions (that is, function templates) will not be instantiated
Function templates must be instantiated by programmers into executable functions
The virtual type name of the function template determines the actual type during compilation.
(7) function overloading
The purpose of programming with overloaded functions is to use the same function name to call functions with similar functions, easy to use and improve readability.
The formal parameters (number or type) of overloaded functions must be different
Void x (int,char ch='a') and void x (int) can be defined in the same program, but cannot be overloaded
(8) inline function
1. Inline function inline:
The purpose of introducing inline function is to solve the efficiency problem of function call in the program; when the compiler compiles, the compiler replaces the call expression of inline function with the function body of inline function, while for other functions, it is replaced at run time. This is actually a space cost for time savings (disadvantages: the code is copied many times, increasing the amount of code, taking up more memory space), so when the function code is small and called frequently. Be careful when using inline functions:
When ① uses a base class pointer or reference to call a virtual function, it cannot be an inline function (because the call occurs at run time). However, when called with an object of a class (not a pointer or reference), it can be treated as inline, because the compiler knows exactly which class the object belongs to at compile time
By default, if the member functions defined in the class body do not include loops and other control structures and meet the requirements of inline functions, C++ will automatically treat them as inline functions (not all member functions are inline functions)
③ inline functions insert the object code of the function at compile time into every place where the function is called, not at run time
④ inline functions do parameter type checking at compile time
⑤ does not allow circular statements (for,while) and switch results in inline functions, and functions with exception interface declarations cannot be declared as inline functions. In addition, recursive functions (calling their own functions) cannot be used as inline functions. Inline functions are only suitable for small functions with only 1-5 lines
The definition of the ⑥ inline function must appear before the first call of the inline function.
Before ⑦ defines the inline function inline write type
The ⑧ keyword inline must be placed with the function definition body to make the function inline. Putting inline in front of the function declaration does not have any effect.
⑨ if the inline function is defined outside the class, the class definition and the member function definition must be placed in the same header file, otherwise it cannot be replaced at compile time
The ⑩ header file must contain not only the declaration of the inline function, but also the definition, and inline must be added to the definition
Whether ⑪ is the inline function defined in the class declaration or the inline function defined in the class implementation, there is no question of priority or non-priority.
⑫ built-in functions do not need to use stack for on-site protection and recovery.
⑬ 's function prototype modified with inline and its corresponding function will also become an inline function-error (self-understanding: inline is a suggested keyword)
⑭ inline functions can be static
This is the end of the introduction on "what are the function concepts in C++". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.