I have never encountered this kind of situation before, and I don't know the details of the actual project. Let's take a small test example and look at the running results:
Int a = 1;
Int B = 2;
Int * tmp = &;
Int * p = tmp; // The second case: int * & p = tmp; (this is a reference to the pointer)
P = & B;
* P = 5;
1. test what a, B, * tmp, * p are at this time: a = 1, B = 5, * tmp = 1, * p = 5;
2. In the second case above, that is, the reference to the pointer, what are the values of these variables? The answer is:
A = 1, B = 5, * tmp = 5, * p = 5;
This is because the reference to the pointer not only changes the object referred to by the pointer, but also changes the pointer itself.
The following uses the code of Essential C ++ P177 as an example:
This code is used to delete a node with a binary tree value equal to a specific value:
1 void BTnode: remove_value (const int & val, BTnode * & prev) // delete a node whose value is _ val = val in the binary Ordered Tree
2 {
3 if (val <_ val) // traverse to the left subtree for search
4 {
5 if (! _ Lchild)
6 {
7 return; // not in this binary tree
8}
9 else _ lchild-> remove_value (val, _ lchild );
10}
11 else
12 if (val> _ val) // traverse to the right subtree for search
13 {
14 if (! _ Rchild)
15 {
16 return; // not in this binary tree
17}
18 else _ rchild-> remove_value (val, _ rchild );
19}
20 else
21 {// haha !~ Found ~ It's you !!
22 if (_ rchild) // check whether the node to be deleted has right children
23 {// indeed, there are ~~ It's a little troublesome ~~ Ah
24 prev = _ rchild;
25 if (_ lchild) // The node to be deleted has a left child ~ You have to find a way to get the left child under the right subtree to delete this node.
26 {
27 if (! Prev-> _ lchild) // check whether there are a bunch of left children in the right subtree
28 {
29 prev-> _ lchild = _ lchild;
30}
31 else BTnode: lchild_lead (_ lchild, prev-> _ lchild); // traverse to the leftmost node of the right subtree
32}
33}
34 else prev = _ lchild;
35 delete this; // do not be afraid to delete this node. My children will not be able to connect to my parent nodes ~~ Prev = _ rchild solves all problems ~~
36}
37}
In the code above, the red part is the most critical part, such as the following tree:
Null indicates that no node exists (I add null to make 6 look like a right child ).
In this region, I want to delete five nodes. If the parameter in the function is a pointer rather than a reference to the pointer. Then, after the five nodes are deleted, the binary tree is broken into two parts. (Prev = _ rchild; here we only change the prev to the value of _ rchild again, but not the value pointed to by the address 5, but in our program, we need to change the address of 5)
If it is a reference pointing to a pointer, prev = _ rchild; this statement will assign the 5 address to the 6 address, so that delete 5 (delete this ;) then, the binary tree will not be broken.
The description of the Essential C ++ P177 is as follows:
Why do we transmit prev with a reference to pointer? Isn't it enough to pass through a simple pointer? No, not enough! Transmitted by pointer, what we can change is what the pointer refers to, not the pointer itself. To make the table pointer itself, we must add an indirect layer. If we declare prev as reference to pointer, we can not only change the pointer itself, but also change the object pointed to by this pointer.
From ziyoudefeng