Click to See Complete Forum and Search --> : confused on what passing by *& is doing!


voidflux
March 22nd, 2007, 10:44 AM
Hello everyone...

I'm confused on what exactly is happening when you pass somthing by pointer reference if thats how you say, *&.

For instance here is the function:

//I think this function is suppose to return a valid pointer, pointing to the root after its built

template<typename T> void BuildTree(TreeNode<T>*& root, const string& epxression)
{
root = NULL;

}


If i had this class:

template<typename T> struct TreeNode
{
TreeNode(const T& value, TreeNode<T>* left = NULL, TreeNode<T>* right = NULL)
{
Value = value;
Left = left;
Right = right;
}

T Value;
TreeNode<T>* Left;
TreeNode<T>* Right;

bool IsLeaf() const
{
return Left == NULL && Right == NULL;
}

};



WOuld I use the function like this?



//main.cpp
TreeNode<char> *root = new TreeNode<char>;
std::string expression = "a+b*4";

BuildTree(root,expression);



So really my question is, if thats what I should be doing in the main function and also, why is it *& and not just &? or not just *?

Thanks, if you have a website that explains that, that would be helpful as well!

TheCPUWizard
March 22nd, 2007, 10:47 AM
Because it is a "pointer by reference".

This not only allows you to modify the entith that is being pointed to, but the pointer itself.

JVene
March 22nd, 2007, 11:11 AM
Early on (like 1987/88, if I recall), many of us new in C++ were tell each other "you could think of this as a pointer to a pointer, just not with the second pointer".

That is, of course, not exactly the best way to think of it, but the result of *& does give you similar ability.

TheCPUWizard
March 22nd, 2007, 11:18 AM
Early on (like 1987/88, if I recall),


More likely 1991 or so unless you were one of the people who was attempting to work with some of the "hacks" that were being produced while Barjne was still developing the language... ;)

JVene
March 22nd, 2007, 01:52 PM
:) I was