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!
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!