In C ++, pointer and reference are two basic concepts.
=============================== Part I ====================== =
Programming is actually a dialogue with the compiler. C ++ programming is nothing more than a dialogue with a compiler that only understands C ++.
According to the C ++ language rules, I have to talk to the compiler so that it can understand:
Mlx FangWei = South China;
Mlx is a certain type (a specific fictional type, in order to make understanding more universal), FangWei is the variable, and the south is the content stored in this variable. Then I said to the compiler:
Mlx * zz1 = & FangWei;
C ++ requires that the variable types on both sides of the value assignment operator "=" must be consistent. Therefore, we must first write a mlx and then use * To tell the compiler that zz1 is a pointer, different from other variables!
So why is there? In fact, the pointer is used to save the box of the "Address" of the specified object (specifically, the "Address" is used to make it more visible, and all objects have an address in the memory ). Here & is the address fetch operation. It is conceptually different from & (used to start an alias) in "Reference! The preceding statement allows the compiler to save the address of FangWei in the memory to zz1.
Well, now I want the compiler to answer me. It's just a conversation! So I said:
Cout <zz1 <endl;
Cout <* zz1 <endl;
The previous sentence asks the compiler to show me the address that zz1 has stored, and the next sentence asks the compiler to tell me what the address actually contains. Note that * this symbol is not meant to express the meaning of "followed by a pointer", but an operator that tells the compiler to perform "unreference operations"! But I personally think it corresponds to and should be called "Object fetch operation" (corresponding to "address fetch operation ")!
Now, the compiler knows what I said and answers me:
0055FB60
South China
The compiler told me that an address 0055FB60 is saved in the zz1 box, and there is a thing called the South.
=============================== Second part ======================== =
If the above sections are correct, the following is my headache, pointer!
Mlx ** zz2 = & zz1;
This sentence means that I tell the compiler to define a pointer zz2, which refers to the object zz1. According to the definition, the pointer is the box that stores the address of the indicated object, then zz2 is the box that stores the address of zz1, but this address is unknown now. It should be displayed through the cout command. Note that this address is not the fb60 address in the south, but the address of zz1. Remember, pointers are also an object, where an object must have a memory address.
So when I finally say:
Cout <zz2 <endl;
This is to let the compiler tell me the address of zz1 itself, that is, which corner of the memory does zz1 live in!
I also said:
Cout <* zz2 <endl;
I added a * operation to perform "unreference" (I understood it as "Object fetch"), and asked the compiler to tell me who lives in this corner-in fact, zz1.
I also said:
Cout <** zz2 <endl;
Add another *, that is, since * zz2 gets zz1, and zz1 is a pointer, it saves the address of the South, so ** zz2 continues to get the object, and it gets the Southern self.
To sum up, the compiler will answer the following three sentences:
0078CC34 // address of pointer zz1
0055FB60 // the address of the variable FangWei, which is also saved in the zz1 box
South China
To sum up, * and &: * are used as pointer type modifiers during definition Declaration. In other cases, for example, when an expression or cout is required, it is used to dereference (take an object). Similarly, & When declaring a definition, it is used as a modifier for reference. In other cases, it is the address operator. You must not make a mistake here.
=============================== Third part ======================== =
The most difficult thing! How does one understand "pointer reference" and "pointer to reference?
Continue to understand the following two concepts: "pointer is the box that saves the address of the indicated object" and "reference is the binding of another name of the indicated object:
A reference to a pointer exists because the pointer itself is an object with a memory address. This reference is bound with an alias!
"Pointer to reference" does not exist! The reference itself is not an object and there is no memory address, so you cannot point to this address with a pointer!
In other words! In addition to saving others' addresses, a pointer also has a place, which is a physical object (so its address can be pointed by another pointer ), but the reference is only the binding of others' names, and there is no actual address. The compiler will not copy the variable content to this name! For example, I reference the Southern name FangWei to FangXiang:
Mlx & FangXiang = FangWei;
Then this FangXiang is just a reference, and this reference is just the alias of FangWei! The South still lives in FangWei, or in 0055FB60! Therefore, the pointer cannot point to FangXiang, but will only point to FangWei!
=============================== Part 4 ======================== =
"Pointer to Pointer" exists
"Pointer reference" exists
"Pointer to reference" does not exist
Conclusion: I always think that the conventional Translation Interpretation greatly hinders beginners from learning. The above three sentences can be clearly described as "another pointer to store a pointer address ", "alias bound to a pointer name" and a pointer that stores the address bound to an object's alias. Of course, the last sentence does not exist. Because the alias bound to an object is not an object and there is no memory address, there is no pointer to this alias. However, this alias has its own ontology, so there is a pointer to this ontology.