Original Post
I'm trying to understand why this copy constructor doesn't work, and how to get it to work if possible.
BinaryNode::BinaryNode( const BinaryNode& argNode )
{
if ( this != 0 && &argNode != 0 ) // How to stop recursion?
{
leftNode = 0, rightNode = 0;
// Only way is to put this in a if statement?
leftNode = new(nothrow) BinaryNode( *argNode.leftNode );
data = argNode.data;
// Only way is to put this in a if statement?
rightNode = new(nothrow) BinaryNode( *argNode.rightNode );
}
}
I read somewhere that "new" never returns null. Is this true? I'm wondering if it's possible to use recursion and use some form of the if statement above to stop it (recursing). Hopefully you get the idea of what I'm trying to do with this code. The reason I hoped this would work is because C++ delete operator can do this: public:
~BinaryNode()
{
delete leftNode; // recursively deletes each child node
delete rightNode;
}
I have a working copy constructor but I'm trying to understand why the code above doesn't work.