C ++ FAQ reading notes [2] - reference

xiaoxiao2021-03-06  41

[8.1] What is a reference? An alias of an object (a replacement name) called reference

The reference is often used in the reference transmission of the function parameters. Void Swap (INT & I, INT & J) {INT TMP = I; I = J; J = TMP;} int main () {INT X, Y; SWAP (X Y);

Here, I and J are main as a reference to functions X and Y. In other words, I is x --- is not a pointer or a copy of X, but the x itself, everything you do is equivalent to the action directly to X, and it is the same.

This is what you should understand as a program. Now, I feel confused by giving you a different perspective, explaining how the reference is implemented. In the bottom layer, one reference I of the object X is the machine address of the object X. But if the programmer executes i , the compiler generates the code that allows X to increase. In particular, the address bits used to look for X did not change. A C programmer will call this imagination as a pointer in C. In other words, the C programmer will put one of the I imagine * P, where P is a pointer to X (for example, the compiler will automatically refer to the underlying instruction; i is converted to become (* P ) ; i = 7 will automatically be * p = 7).

Note: Even references are often implemented by using the assembly language using the underlying, please do not imagine an imagination into a strange pointer of an object, a reference is an object. It is not a pointer to the object, nor is an object a copy. It is that object.

[8.2] What happens if you assign a reference to a reference? You can change the state of a reference body (reference refernt is the object introduced by reference)

Remember: The reference is the reference body, and if the reference is changed, you can change the status of the reference. The term is called by the compiler, and a reference is a "left value" ("LVALUE") (就 是 西 东 西 西 西)

[8.3] What happens when you return a reference?

Function calls can appear on the left end of the assignment operator.

Such ability may be strange. For example, no one will feel f () = 7 meaningful. However, if A is an object in an object array, many people will feel that A [i] = 7 is meaningful or even a [i] is actually a camouflage function call (it is data [] call: Array :: Operator [] (int), it is the operator of a sub-script of the array class).

Class array {public: int size () const; float & operator [}; // ..

INT main () {array a; for (int i = 0; i a [i] = 7; // This line calls array :: Operator [] (int)}

[8.4] How do I get a reference to re-reference to another object? This is impossible

You can't separate the references from the reference

Not the same as the pointer, if a reference is bound to the object, it can re-assapose to another object. If the reference object itself is not an object (it does not have a flag information, the address acquired is the address of the reference body; remember, the reference is the reference body yourself)

In this sense, a reference to a normally pointer, such as INT * Const P is similar (opposite to a pointer to constant constant like const Int * P). Despite these similarity, please do not put the reference and pointer Six, they are completely different.

[8.5] ​​When do I use a reference, when is the pointer? As long as you can use the reference, you must use the pointer.

When you don't need to re-assign, use usually better solutions than pointers. This is usually said that the reference is the most useful part of a class of public member interfaces. Quote often display an outer table of an object, and pointer is usually internal content. One exception to the above is a "guard" reference when a function parameter or return value is required. It is often implemented by returning or accepting a pointer, which is considered to be the best implementation, then gives an empty pointer to cause trouble (reference is always associated with the object, not an empty pointer) .

Note: An old C programmer sometimes does not like to reference because it provides a reference method that has been clearly explained in the call code. However, after having some C programming experience, you can quickly realize that this is a hidden approach, which is a property, not a responsibility. For example, programmers should write code for issues instead of writing code for machine language.

[8.4] How do I get a reference to re-reference to another object? This is impossible

You can't separate the references from the reference

Not the same as the pointer, if a reference is bound to the object, it can re-assapose to another object. If the reference object itself is not an object (it does not have a flag information, the address acquired is the address of the reference body; remember, the reference is the reference body yourself)

In this sense, a reference to a normally pointer, such as INT * Const P is similar (opposite to a pointer to constant constant like const Int * P). Despite these similarity, please do not put the reference and pointer Six, they are completely different.

[8.5] ​​When do I use a reference, when is the pointer? As long as you can use the reference, you must use the pointer.

When you don't need to re-assign, use usually better solutions than pointers. This is usually said that the reference is the most useful part of a class of public member interfaces. Quote often display an outer table of an object, and pointer is usually internal content.

One exception to the above is a "guard" reference when a function parameter or return value is required. It is often implemented by returning or accepting a pointer, which is considered to be the best implementation, then gives an empty pointer to cause trouble (reference is always associated with the object, not an empty pointer) .

Note: An old C programmer sometimes does not like to reference because it provides a reference method that has been clearly explained in the call code. However, after having some C programming experience, you can quickly realize that this is a hidden approach, which is a property, not a responsibility. For example, programmers should write code for issues instead of writing code for machine language.

[8.4] How do I get a reference to re-reference to another object? This is impossible

You can't separate the references from the reference

Not the same as the pointer, if a reference is bound to the object, it can re-assapose to another object. If the reference object itself is not an object (it does not have a flag information, the address acquired is the address of the reference body; remember, the reference is the reference body yourself)

In this sense, a reference to a normally pointer, such as INT * Const P is similar (opposite to a pointer to constant constant like const Int * P). Despite these similarity, please do not put the reference and pointer Six, they are completely different.

[8.5] ​​When do I use a reference, when is the pointer? As long as you can use the reference, you must use the pointer.

When you don't need to re-assign, use usually better solutions than pointers. This is usually said that the reference is the most useful part of a class of public member interfaces. Quote often display an outer table of an object, and pointer is usually internal content.

One exception to the above is a "guard" reference when a function parameter or return value is required. It is often implemented by returning or accepting a pointer, which is considered to be the best implementation, then gives an empty pointer to cause trouble (reference is always associated with the object, not an empty pointer) Note: An old C programmer sometimes does not like to quote because it provides a reference method that has been clearly explained in the call code. However, after having some C programming experience, you can quickly realize that this is a hidden approach, which is a property, not a responsibility. For example, programmers should write code for issues instead of writing code for machine language.

转载请注明原文地址:https://www.9cbs.com/read-60275.html

New Post(0)