Hey, folks,I met a block fo code in my book, and I don't understand why we need a reference to pointer? It seems like pointers can do anything that references to pointer do? Can you explain it and give me a example? Thanks!
Like a reference to any basic type, you need it if you want to change it's value.
With a reference to pointer you can both modify the value pointed and the position pointed
I guess what you might like to hear is that BTnode *&prev is a reference to a pointer. In other words, the method expects a pointer and it does not copy the actual pointer, it takes a reference to it, which does not change the value being pointed to. Additionally, as Bazzy mentioned, lines 24 and 30 modify the pointer--meaning that the original pointer in the call to the function is being changed (via it being passed by reference).
Consider the following simplification (no templates):
1 2 3 4 5 6 7 8
int x;
BTnode * node;
// initialize x and node somewhere
// call the function
remove_value( x, node );
//...
On line 4, node equals the address of a BTnode object. Assuming that line 24 or 30 executed inside the call to remove_value, then on line 7, node equals the address of a different BTnode object--meaning that the address stored in node has been changed outside of the scope of the function call.