Abstract: Introducing the basic concepts of C references, through detailed application analysis and description, the reference is fully, thoroughly explained. Keywords: reference, const, polymorphism, pointer reference is a new language characteristic introduced by C , one of the important contents of C common content, correct, flexible use of reference, can make the program simple, efficient. I found it in my work. Many people use it just to be of course, in some subtle occasions, it is easy to make mistakes. Most of them are due to this source. Therefore, in this article, I will discuss the reference in this article, I hope to better understand and use the reference to the role of the brick jade. Quote Introduction The reference is an alias of a variable (target), which is exactly the same as the direct operation of the reference. Quote: Type Identifier & Reference Name = Target Variable Name; [Example 1]: Int A; INT & RA = A; / / Define Reference RA, it is a reference to variable A, ie alia name description: (1) & It is not an address operation here, but a logo. (2) Type identifiers refer to the type of target variable. (3) When the declaration is referenced, it must be initialized simultaneously. (4) After the reference declaration is completed, it is equivalent to the target variable name, which is the target original name and reference name, and can no longer use the reference name as an alias of other variable names. RA = 1; equivalent to a = 1; (5) declare a reference, not a new variable, which only indicates that the reference name is an alias of the target variable name, which is not a data type, so reference it itself No storage unit, the system does not assign storage units to the reference. Therefore, the address is requested to be addressed to the target variable. & ras equivalent to & a. (6) Can't establish an array reference. Since array is a collection consisting of several elements, an alias of an array cannot be established. Quote Application 1, reference to an important role as a parameter reference is the parameter of the function. The function parameters pass in the previous C language are values. If there is a large block of data as a parameter transmission, the scheme used is often a pointer, as this can avoid all stacking of the entire data, and can improve the efficiency of the program. But now (C ) has added a choice of equivalent efficiency (in some special cases, it is necessary to choose), is a reference. [Example 2]: Void SWAP (INT & P1, INT & P2) // These functions of this function are referenced to {INT P; P = P1; P1 = P2; p2 = p;} is called in the program This function, at the invocation point of the corresponding main adjustment function, directly as the argument, without any special requirements, without requiring the real parameters. Such as: corresponding to the SWAP function defined above, the corresponding main adjustment function can be written as: main () {INT A, B; CIN >> A >> B; // input A, B two variables of the value SWAP (A, B ); // Directly use the variables A and B as a solid-parap with SWAP function cout << a << '<< b; // output result} The above program is running, if the data 10 20 is input, then output The result is 20 10.
As can be seen from [Example 2]: (1) Transfer reference to function is the same as the effect of the transfer pointer. At this time, the parameter of the modulated function is used as an alias for the actual argument variable or object in the main modulation function, so the operation of the counter-arranging variable in the modulated function is the corresponding target object (at the main) The operation of the modulus of the function. (2) Use the parameters of the reference transfer function, there is no copy of the argument in memory, which is directly for the actual parameters; and the parameters of the general variable transfer function, when the function is called, the formation is required to distribute storage The unit, the variable variable variable is a copy of the real parameter variable; if the object is passed, the copy constructor will also be called. Therefore, when the data passed by the parameter is large, the efficiency of transmitting parameters with general variables with general variables is good. (3) Although the parameter using the pointer as a function can also achieve the effect of using the reference, it is also necessary to give the ginseng storage unit in the modular function, and need to repeat the "* pointer variable name" in the form of operation, This is easy to generate errors and is poorly reading; on the other hand, at the call point of the main adjustment function, the address of the variable must be used as the argument. The reference is easier to use, clearer. If you need to use the reference to improve the efficiency of the program, it is necessary to protect the data that is passed to the function is not changed in a function, and a common reference should be used. 2, often citing often reference declaration: const type identifier & reference name = target variable name; reference to this way, cannot be modified by reference to the value of the target variable, so that the target of the reference is constant, reaching Quote security. [Example 3]: Int a; const INT & ra = a; ra = 1; // error a = 1; // correct this is not only to make the code more robust, and some other aspects. [Example 4]: Assume that there is a function declaration: string foo (); void bar (String & S); then the following expression will be illegal: bar (foo ()); bar ("hello world); reason In the foo () and "Hello World" strings generate a temporary object, in C , these temporary objects are const types. Therefore, the above expression is to try to convert a const type of object to a non-const type, which is illegal. The reference parameters should be customized as constings without CONST. 3. Reference as a return value to return function values to reference, the function is defined as the following format: Type Identifier & Function Name (Description of Types and Types Description) {Functional Body} Description: (1) To reference the return function value The biggest advantage of the definition function is required to return to a function value before the function name is that a copy of the returned value is not generated in the memory. [Example 5] A normal function fn1 (which returns a function value with the return value) in the following program, and another function fn2 returns a function value in a reference method.
#include
The << operator cannot be used continuously for returning a stream pointer. Therefore, returning a stream object is the only choice. This unique choice is critical, which illustrates the importance of reference and irreplaceability, maybe this is the reason why the C language introduces this concept. Assignment operator =. Like the operation of the operation, it is possible to use, such as x = j = 10; or (x = 10) = 100; the return value of the assignment operator must be a left value so that it can be continued to assign. Therefore, reference to the unique return value of this operator. [Example 6] Tests the value of the returned reference to the left value of the assignment expression. #include