inline functions
The difference between an inline function and a regular function is:
1. General function : When executing the calling instruction, store the memory address of the instruction, copy the function parameters to the stack, then jump to the memory unit where the function is called, execute the function, put the return value in the register, and then jump back to the command that was saved at the beginning.
2. inline function : The compiler replaces the function call with the function code, does not need to jump, but the memory overhead is large.
PS: The inline function cannot be recursive, and the function body of the inline function cannot be too large. The keyword inline is added to the declaration and definition before use.
Reference pass-through value
When a function's argument is a const reference, the compiler generates a temporary variable in the following two scenarios:
1. Argument types do not match, but can be converted to the correct type.
2. Argument types match, but not lvalue.
When using temporary variables, the behavior is similar to passing by value, and the original data is not modified and temporary variables are used.
PS: A reference to a base class can point to a derived class object.
Default parameters
Default parameters are given in the function prototype, and default values must be added from right to left.
function overloading
The key is that the argument lists are different, and type references and types are treated as the same feature labels when overloaded.
You cannot cast a match when there are multiple overloaded functions at the same time.
Overloaded functions are name-decorated at compile time to differentiate between overloaded functions.
Template functions
The templates here are also called parameterized types.
A value similar to swapping two variables of any type:
1 template<class t>2void tswap (t &a, T &B)3{ 4 T temp; 5 temp = A; 6 A = b; 7 b = temp; 8 }
The compiler generates the appropriate function at compile time based on the type of parameter that is used at the time of the call. If you use int, replace T with int to generate a function.
This means that the template does not shorten the length of the final compiled file, and it has the advantage of safely generating multiple functions of the same function and reducing the amount of manual code written.
Function templates can also be overloaded, such as:
1 template<class t>2void tswap (T &a, t&b); 3 4 template<class t>5voidint n);
However, when the parameter list is the same and you want to invoke a specific function for a specific type, you can use the display to materialize or display the instantiation:
1 //function Templates2template<classT>3 voidTswap (T &a, t&b);4 5 //Show materialized6Template<>voidTswap<classname> (ClassName &a, ClassName &B);7 //or8Template<>voidTswap (ClassName &a, ClassName &B);9 Ten //Display instantiation OneTemplatevoidTswap<classname> (ClassName &a, ClassName &b);
C + + supplements (vii) function correlation (2)