JavaBoy

Hi,
For the below code snippet, I wonder about the function of *& in parameter
How is it different from " * " in terms of their function

void BinaryTree::destroyTree(TreeNode *& treePtr) {
// postorder traversal
if (treePtr != NULL)
{ destroyTree(treePtr->leftChildPtr);
destroyTree(treePtr->rightChildPtr);
delete treePtr;
treePtr = NULL;
} // end if
} // end destroyTree



Thanks



Re: Visual C++ Express Edition *& and * difference

einaros

JavaBoy wrote:
For the below code snippet, I wonder about the function of *& in parameter
How is it different from " * " in terms of their function
The base difference between * and & is that * declares a pointer, and & declares a reference.
If you're not familiar with references, they are essentially aliases. A reference to a piece of data *is* the piece of data. References also cannot be reassigned to reference something else, really because they *are* the data. Reassigning them wouldn't change the reference, it'd change the data!
Now that the reference part has been pointed out, I'll give a syntax example
int* p; // pointer to int
int& p; // reference to an int
int*& p; // reference to a pointer to an int
So that's fairly straight forward, and it should give you some clues as for what happens with your function call. Another example:
void foo(int* p)
{
p = NULL;
}
void bar(int*& p)
{
p = NULL;
}
int myInteger = 100;
int* intPointer = myInteger;
// intPointer points to myInteger
foo(intPointer);
// intPointer still points to myInteger
bar(intPointer);
// intPointer now "points" to the 0 address
In case of the foo function above, what's being passed isn't actually the intPointer, it's a copy of the intPointer variable. A pointer variable can essentially be thought of as a number, where the value is a memory address somewhere. When you call a function which accepts a simple pointer, such as foo above, only that number is passed along.
In case of bar, however, we pass a reference to a pointer. That means that the actual pointer variable will be changed. When you assign a new value to that reference, the new address is assigned to the actual intPointer. We've effectively changed the pointer that was passed to bar.
The rule of thumb here is that when you pass a value, you pass a copy of a number, a copy of a character or similar. When you pass a reference, you pass along the actual object. When a function accepts a non-const reference, that means that the function can change the variable, and that's the case with the example you described in your original post.
I hope this all made sense to you





Re: Visual C++ Express Edition *& and * difference

Dreedle

As you know * specifies a pointer - the *& is a pointer-to-a-reference; i.e. double indirection similar to ** (pointer-to-a-pointer). It allows the method to internally set the treePtr parameter to null. Without the double indirection the method would not be able to nullify the treePtr parameter because parameters are passed by value (copied on the stack) .






Re: Visual C++ Express Edition *& and * difference

einaros

Dreedle wrote:

*& is a pointer-to-a-reference.

Actually, it's a reference to a pointer Pointers to references are quite different (not to mention illegal) creatures.






Re: Visual C++ Express Edition *& and * difference

JavaBoy

Thank you.
These really help me.

Again thanks for the clarification.

Best wishes