|
From: Socha, D. F <dav...@us...> - 2003-06-09 11:36:53
|
With an assignment operator for a user defined class, a binary Search =
Tree class for example, would it be a problem if my overloaded =
assignment operator only handled cases where you were assigning one tree =
to another.
=20
Here is some sample code:
/************************************************************************=
****
Overloaded assignment operator
Used for: assigning the value of one tree to another tree
Sent: A reference to the original tree
Returns: A reference to a tree
Works by: First checking to see if the tree bieng copyied to has data in =
it.
If it does, the deleteTree function is called to empty the tree/free up =
all the
memory in the tree.
Second, checks to see if the tree being copyied has any data in it. =
If it
does, the copyTree function is called to copy the data from the original =
tree to
the copy tree. If the original tree does not have data on it (root =
=3D=3D NULL)
then the root of the copy tree will be set to null.
*************************************************************************=
***/
template<typename T> BST& BST<T>::operator=3D (const BST& origTree){
if(root){
destroyTree(root);
}
=20
if(orig.root){
copyTree(origTree.root, root);
}
else{
root=3DNULL;
}
=20
return *this;
}
//End operator=3D
/************************************************************************=
****
copyTree function
Used by the copy constructor / asignment operator to copy the tree
Sent: pointer to the original node, pointer to the node being copied to
Returns: Nothing
Works by: Creating a new node on the tree being copied, copy the data to =
the
node, checks to see if a left node exists on the tree, if yes
repeat the process using on the left node, if not, do nothing. Then =
checks
the right, if it exists repeat process on the right side, if not, do =
nothing
NOTE: This function is a recursive process
*************************************************************************=
***/
template<typename T> void BST<T>::copyTree(Node* origNode, Node* =
copyNode){
copyNode =3D new Node(origNode->data);
=20
if(origNode->left){
copyTree(origNode->left, copyNode->left);
}
=20
if(origNode->right){
copyTree(origNode->right, copyNode->right);
}
}
//End copyTree
=20
I am using the version 4.9.8.0 of Dev C++
=20
Should this work or will I have a problem with this.
Thanks,
David Socha
|