1, what is the alias of the reference? The object of the object (another name). 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 to a programmer. Now give you a different angle, this may make you more confused, that is how the reference is achieved. 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. Second, give the 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. In the case of the compiler, the reference is a "left value" (it can appear on the left side of the assignment operator). Third, return 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) } Four, Object.method1 (). Method2 () What does it mean? Connect the call to these methods, thus being called the method chain first 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 can perform because cout << X is a function of returning COUT. Although there is fewer use, it is still skilled in the Named Parameter Idiom to use the method chain. 5. How can I get a reference to 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. 6. When will I use a reference, when should I use a 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. 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? The genre term is generally used to refer to the method of acquiring another object - a generalized fake pointer. This term is (deliberate) ambiguous. Very vaguely clear is useful in some cases of actual. 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