what is a reference to pointer intended to do?

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!
A reference to a pointer is like a reference to any other type.
Can you post the code you don't understand?
Ok,the code is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
template <typename valType>
void 
BTnode<valType>::
remove_value( const valType &val, BTnode *&prev ) 
{
    if ( val < _val )
    {
         if ( ! _lchild )
              return; // not present
         else _lchild->remove_value( val, _lchild );
    } 
    else 
    if ( val > _val )
    {
         if ( ! _rchild )
              return; // not present
         else _rchild->remove_value( val, _rchild );
    } 
    else 
    { // ok: found it
      // reset the tree then delete this node
      if ( _rchild ) 
      {
         prev = _rchild;
         if ( _lchild )
			  if ( ! prev->_lchild )
				   prev->_lchild = _lchild;
			  else BTnode<valType>::lchild_leaf( _lchild, prev->_lchild );
      }
      else prev = _lchild;
      delete this;
    }         
}


What i don't understand is that why we need a reference to pointer here? I think a pointer is sufficient...Can you explain ? Thanks!
lines 24 and 30 modify the value of the pointer ( not the value pointed by it ), you need a reference to do so
So is there other examples where you would use a reference Bazzy?

or is it pretty much just used to change the value of the pointer, rather than the value it points to.
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
All right, I get it ! Thanks ,Bazzy.
akilguo wrote:
BTnode *&prev

can anyone explain this to me? a pointer to a reference? or dereferencing a reference?
Isn't the title clear?
what is a reference to pointer intended to do?

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.
Last edited on
i mean is BTnode a pointer to a reference? or a reference to a pointer?
Topic archived. No new replies allowed.