Another talk about C + + functions
1: References
2: Default parameter
3: Function overloading
4: function template
————————————————————————————————————————————————————————————————
1: References
Reference is a new language feature introduced by C + +, which is one of the most important contents in C + +, and the correct and flexible use of reference can make the program concise and efficient.
represents an alias to a variableTwoshare the same memory area。 Similar to the same person with different names.
References as function arguments
1. In order to modify the arguments inside the function.
2. When a large structure or object is used as a function parameter, theAvoid copyingWe generally use pointer or reference pass parameter .
Reference as function return value
Returns the function value, which is required when defining the functionThe name of the function is preceded by a &: Specifically, add &
The benefit of returning a reference type isno copy, no copy required。 Becausecopying requires a lot of resources。
Note: You cannot return a reference to a local variable. The reason is that local variables are destroyed after the function returns
Some summaries of the references
(1) In the use of reference, it is meaningless to give a variable a single name, the purpose of the reference is mainly used infunction Parameter PassingTo resolve large chunks of data orthe efficiency of object transfer and the unsatisfactory spaceThe problem.
(2) using the parameters of the reference transfer function, canensure that no replicas are generated in the parameter pass,improve the efficiency of delivery, and throughThe use of const guarantees the security of reference passing。
(3) The difference between a reference and a pointer is when the pointer points to an object through a pointer variable, the variable to which it pointsIndirect Operation。The use of pointers in programs, the readability of the program is poor;The reference itself is the alias of the target variable, and the action on the reference is the action on the target variable.
Reference code:
————————————————————————————————————————————————————————————————
(2) Default parameters
The default parameter value of the function, which is to give it a default initial value at the same time as the parameter is defined. When calling a function, we can omit the parameter that contains the default value. That is, if the user specifies a parameter value, the user-specified value is used, otherwise the value of the default parameter is used.
The default parameter syntax is used with:
(1) Assign a default value to a parameter when the function is declared or defined.
(2) When the function is called, omit the parameter with the default value. You can use default parameters instead.
If a parameter is set to a default value, the parameter to the right will have a default value.
You can specify a default parameter in a function declaration or function definition, but only one place, cannot be specified at the same time in two places
}
————————————————————————————————————————————————————————————————
(3) Function overloading
function overloading means that within the same scope, there can be a set of functions with the same function name and different parameter lists , which are called overloaded functions .
Overloaded functions are often used to name A set of functionally similar functions , which reduces the number of function names , avoids the pollution of namespaces, and has great benefits for the readability of the program.
It is important to note that if the two function parameter lists are the same, only the return value is different, this is not an overload, the compiler will error when compiling.
10 + 20
10.3 + 20.3
————————————————————————————————————————————————————————————————
(4) Function template
The so-called function template, is actually to create a general function, its function return value type and formal parameter type is not specified, with a virtual type to replace. This general function is called a function template .
Instantiation of a function template
The function template is defined in the following form:
Template <typename T>: where template and TypeName are keywords, remain unchanged, variable can only be T
function implementation
Comparison to function overloading
The template function is somewhat similar to the function overload, but the difference is obvious.
The internal implementation of an overloaded function can be completely different, but the template function has the same internal algorithm .
C + + Note one: functions