|
-
March 22nd, 2007, 10:44 AM
#1
confused on what passing by *& is doing!
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:
Code:
//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:
Code:
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?
Code:
//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!
Last edited by voidflux; March 22nd, 2007 at 10:47 AM.
-
March 22nd, 2007, 10:47 AM
#2
Re: confused on what passing by *& is doing!
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.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 22nd, 2007, 11:11 AM
#3
Re: confused on what passing by *& is doing!
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.
-
March 22nd, 2007, 11:18 AM
#4
Re: confused on what passing by *& is doing!
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...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 22nd, 2007, 01:52 PM
#5
Re: confused on what passing by *& is doing!
I was
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|