[8] Quote (Part of C FAQ Lite, Copyright © 1991-2001, Marshall Cline, Cline@parashift.com)
体,, nicrosoft @ sunistudio.com (East day production room, East day document)
FAQS in section [8]:
[8.1] What is a reference? [8.2] What does it mean to reference? [8.3] Returns a reference means? [8.4] Object.method1 (). What does Method2 () mean? [8.5] How can a reference point to another object? [8.6] When should the usage reference, when should the usage pointer? [8.7] What is the handle of an object? Is it a pointer? Is it a reference? Is it a pointer to a pointer? what is it?
[8.1] What is a reference?
The alias of the object (the name of the change).
Quote is often used in "PASS-BY-REERENCE":
Void Swap (INT & I, INT & J) {INT TMP = I; I = J; J = TMP;} int Main () {Int x, Y;
// ...
SWAP (X, Y);
The I and J here are X and Y in Main. In other words, I is x - not pointing to the X-axis, nor is the copy of X, but the X itself. Any change to i will also affect X and vice versa.
Ok, this is a reference as a programmer. Now give you a different perspective, this may make you more confused, this is how the reference is implemented. Typically, the reference I of the object X is the machine address of X. However, when the programmer writes i , the compiler generates an increase in X-based code. In more detail, the compiler is used to find X's address bits and are not changed. The C programmer thinks this seems to be a C style, pressing the pointer, but the syntax (1) will be moved from the caller to the caller, (2) eliminates * s. In other words, the C programmer will see I as a macro (* P), and P is pointing to the X-based pointer (for example, the compiler will automatically release the potential pointer; I is changed to (* p) ; i = 7 is automatically converted into * p = 7).
It is important: Do not see the reference as a singular pointer to an object, even if the reference is often implemented in the address of the assembly language. Quote is an object. It is not a pointer to the object, nor is it a copy of an object, which is an object.
[TOP | BOTTOM | Previous Section | Next Section]
[8.2] Give reference to the reference, what does it mean?
Change the referenced "Indicator" (reference to the object referred to).
Remember: The reference is its indicator, so when it changes the reference value, the value of its indicator will also change. According to the compiler author's happen, the reference is a "left value" (it can appear on the left side of the assignment operator).
[TOP | BOTTOM | Previous Section | Next Section]
[8.3] Returns a reference, what does it mean?
It means that the function call can appear on the left side of the assignment operator.
Initially this ability looks a little quirky. For example, no one will think expression f () = 7 is meaningful. However, if a is an Array class, most people will think that A [i] = 7 is meaningful, even if a [i] is actually a function called camouflage (it calls the following Array class Array :: Operator [ ] (int)). Class Array {public: int size () const; float & operator [] (;
// ...
}; Int main () {Array A; for (int i = 0; i // This line calls Array :: Operator [] (int) } [TOP | BOTTOM | Previous Section | Next Section] [8.4] Object.method1 (). What does Method2 () mean? [Recently Created Thanks to Robert Cullen (on 4/01). Click Here to Go To The Next Faq in The "Chain" of Recent Changes ] Connect the call to these methods, is therefore referred to as a method chain The first executed is Object.method1 (). It returns an object, which may be a reference to an object (eg, Method1 () may end with return * this) or may be some other objects. Let's call the object returned to Objectb. Then ObjectB becomes the THIS object of Method2 (). The most common place for the method chain is the iostream library. For example, cout << x << y Works Because cout << x is a function that returns Cout. Although there is fewer use, it is still skilled in the name of the Named Parameter IDiom. [TOP | BOTTOM | Previous Section | Next Section] [8.5] How can I re-point another object? Not. You can't make the reference to separate from its indicator. Unlike pointers, once references and object bindings, it cannot be re-point to other objects. Quote itself is not an object (it is not identified; when trying to get the referenced address, you will go to its indicator address; remember: The reference is its indicator). In a sense, a Const pointer such as INT * Const P is referenced (not a pointer such as const INT * P). No matter how similar, please do not confuse the reference and pointers; they are completely different. [TOP | BOTTOM | Previous Section | Next Section] [8.6] When should the usage reference, when should the usage pointer? Use the reference to use as much as possible, you must use the pointer. When you don't need to "rebest", the reference generally takes precedence over the pointer. This usually means more useful when referenced for public interfaces for class. The typical occasion of the reference is the surface of the object, and the pointer is used inside the object. The above exception is that the parameter or return value of the function requires a "critical" reference. It is usually necessary to return / acquire a pointer and use a NULL pointer to complete this special mission. (Quote should always be alias the object, rather than being released NULL pointer). Note: Due to the clear reference semantics from the caller's code, the traditional C programmer sometimes doesn't like reference. However, when there are some C experience, you will soon realize that this is a form of information hidden, it is beneficial rather than harmful. Just like, programmers should write code for issues to be solved, not the machine itself. [TOP | BOTTOM | Previous Section | Next Section] [8.7] What is the handle of an object? Is it a pointer? Is it a reference? Is it a pointer to a pointer? what is it? [Recently Created (on 4/01). Click Here to Go To The Next FAQ in The "Chain" of Recent Changes ] The genre term is generally used to refer to the method of acquiring another object - a generalized fake pointer. This term is (deliberate) ambiguous. Virgin is useful in some cases in practice. For example, in an early design, you may not be ready to use your handle. You may not be sure if a simple pointer or reference or pointing to a pointer or a pointer pointing to a reference or integer identifier is placed in an array or string (or other key) to enable a hash-table. (Or other data structure) or database keys or some other techniques to query. If you only know that you will need some unique identity to get an object, then these things are called handles. Therefore, if your ultimate goal is to let the code unique identity / query the specified object of a Fred class, you need to pass a Fred handle. The handle can be a string that can be used as a key (key) as a well-known query table (for example, in std :: map Beginners often consider pointers, but actually use uninited pointers with the bottom level. For example, what if the Fred object needs to be moved? How do we know when the Fred object can be securely deleted? What should I do if the Fred object needs (temporary) continuous from the disk? and many more. Most of these times, we add an indirect layer to manage the location. For example, the handle can be fred **, pointing to the pointer to the fred * guarantee that it will not be moved. When the Fred object needs to move, you can update the pointer pointing to Fred *. Or use an integer as a handle, then query the Fred object (or pointing to the fly object) in the table or array or other part. The focus is when we don't know the details of what you want to do, use your handle. Another timing using the handle is to "sometimes using the term Magic Cookie, just like this," software is transmitted, "software is transmitted," "Software passes a Magic cookie to uniquely identify and locate the appropriate Fred object"). The reason why the already completed thing is to minimize the special details of the handle or when the representation is changed. For example, we don't want to update a large number of code when you get a handle to query the integer in the array from the string used in the table. When the details or representations of the handle changes, the maintenance work is simpler (or easier reading and writing code), so it is often encapsulated in the class. Such types often overloads Operator-> and Operator * operators (since the effect of the handle is pointer, it may be tailed to the pointer). [TOP | BOTTOM | Previous Section | Next Section] E-mail the author [C FAQ Lite | Table of Contents | Subject Index | About The Author | © | Download Your Own Copy] Revised Apr 8, 2001