I. References
(1) The reference is to alias a variable.
Define the general format of reference: Type & reference name = variable name;
For example, int A = 1; Int & B = A; // B is the alias of A, so a and B are the same unit.
Note: initialization is required to define the alias of the referenced variable.
In practical applications, references are generally used for parameter passing and return values.
Reference is not a variable. Reference is just an alias of a variable. The reference does not have its own independent space. The reference must share the space with the referenced variable, the changes made to the reference are actually changes to the variables referenced by the reference. The reference must be initialized during definition. Once the reference is initialized, it cannot point to other variables again.
(2) const reference
A const reference is a reference to a const object.
Const int ival = 1024;
Const Int & refval = ival; // OK: both reference and object are const
Int & ref2 = ival; // error: nonconst reference to a const object
(3) transfer by reference
The reference transfer method is to add the reference operator "&" before the form parameter during function definition "&"
Example: swap (Int & A, Int & B );
The pass-by-value method is easy to understand, but the change of the parameter value cannot affect the real parameter.
The address transfer method changes the real parameters by changing the form parameters, but the program is prone to errors and difficult to read.
Any operation on the parameter as a parameter can change the data of the corresponding real parameter, making the function call convenient and natural.
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
# Include <iostream> Using namespace STD; Void swap (Int & X, Int & Y ); Int main (void) { Int A, B; A = 10; B = 20; Swap (A, B ); Cout <"A =" <A <"B =" <B <Endl; Return 0; } Void swap (Int & X, Int & Y) { Int temp; Temp = X; X = y; Y = temp; } |
Note: When referenced as a parameter, the actual participating parameter of the function uses a storage unit in the memory. Therefore, the variation of the form parameter changes the real parameter at the same time.
(4) reference as return value
Another function of reference is to return the referenced function.
A major purpose of function return reference is to place the function on the left of the value assignment operator.
Note: References to local variables cannot be returned. For more information, see my article. The article also compares the relationship between references and pointers.
(5) differences between references and pointers
Direct access to a variable is referenced, while indirect access to a pointer.
A reference is the alias of a variable. Instead of allocating its own memory space, the pointer has its own memory space.
References cannot reference other variables once initialization, but pointers are acceptable.
C ++ recommends that you use references whenever possible and use pointers when you have.
Ii. inline functions
(1) inline functions
When a program executes a function call, the system needs to establish a stack space, protect the site, PASS Parameters, and control the transfer of program execution. Such work requires the system's time and space overhead. In some cases, functions are simple and the code is short, but the usage frequency is very high. It takes a lot of time for the program to call the function frequently, thus reducing the execution efficiency of the program.
To improve efficiency, one solution is to embed the Function Code directly into the program without using a function. You can use macro-defined implementations with parameters, but this method also has disadvantages, program readability is often not good with functions, and some parentheses may be ambiguous.
To coordinate the contradiction between efficiency and readability, C ++ provides another way to define inline functions by using the modifier inline when defining functions. The Inline keyword tells the compiler that the call of this function should be as fast as possible. It can be implemented as a common function call or a macro extension. The Inline keyword is also introduced in c99.
(2) differences between inline functions and macros with Parameters
Inline int max (int A, int B)
{
Return A> B? A: B;
}
# Define max (A, B) (a)> (B )? (A): (B ))
When calling an inline function, the real parameter and the form parameter must be of the same type. In addition, the inline function evaluates the real parameter expression and then passes it to the form parameter. If the real parameter expression has side effect, then these sideeffect occurs only once. For example, if Max (++ A, ++ B) is a real function, A and B are added only once. In macro calls, only the real parameters are used to replace the form parameters. If Max is the macro definition above, it should be expanded to K.
= (++ A)> (++ B )? (++ A) :( ++ B) it is hard to say the number of times a and B increase.
Inline functions expand the code during compilation and calling, while parameter macros are replaced during preprocessing. Therefore, the generated target file is large.
We recommend that you use the inline function in C ++ to replace the macro with parameters.
Three or four types of conversion
Refer to my article.
Refer:
C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications