0

I'm working on an AVL tree with given code, and I get pointers and I get references but I don't get this:

void remove(const Comparable & x, BinaryNode * & t) {

BinaryNode * & t

This appears to me as a pointer to a reference? But searching StackOverflow yields:

Difference between pointer to a reference and reference to a pointer

Pointer to a reference

This doesn't exist. As stated earlier, a reference is merely an alias to another object. You can't "point" to a reference, because it isn't an object in itself but merely another name for a real object.

I strongly suspect this is a question answered many times but searching for C++ * & is rather difficult.

HandsomeRob
  • 445
  • 3
  • 7
  • 14
  • Read it the other way around: `BinaryNode * &` is a reference to a pointer to a `BinaryNode` object. – Some programmer dude May 31 '23 at 04:09
  • @Someprogrammerdude - cheers, wanna answer so I can hit the check else I'll just delete this? – HandsomeRob May 31 '23 at 04:12
  • A pointer is like any other variable - the only thing distinguishing a pointer from other variables is that its value is an address (of another variable, of an object, etc). Passing a pointer by reference means the same as passing any other variable by reference. If the reference is not `const` qualified, it can be used to change the pointer (e.g. to point at something else). There is no such thing as a pointer to a reference though. `BinaryNode *&t` means that `t` is a reference to a pointer (it is equivalent to `(BinaryNode *) &t` not to `BinaryNode *(&t)` [which is not allowed]). – Peter May 31 '23 at 04:25
  • So, the rule of thumb for determining a type is that from the name, sweep right, then sweep left. There is nothing to the right of `t`, so sweeping left gives you a reference to a pointer to a `BinaryNode`. Way easier than the spiral rule. – sweenish May 31 '23 at 04:31

0 Answers0