Link to home
Start Free TrialLog in
Avatar of TheSnowman
TheSnowman

asked on

Null

Is there a way to have an object reference hold "null" like there is in Java? For example:

MyClass x;
x = null;
return x;

I am programming a binary tree. If a node is queried as to whether or not it has a left/right child node, I need to be able to tell if the link points to another subtree, a leaf, or if it is empty (holds "null").
Avatar of Triskelion
Triskelion
Flag of United States of America image

Yes.
Use all caps

char * GetNull(void)
{
   static char * spData=NULL;
   return spData;
}
Avatar of TheSnowman
TheSnowman

ASKER

This is not really what I was looking for... I will clarify.

Basically, this function will work in Java (except for the function header) but will not compile with C++:

bool BSPTree::hasLeftChild() {
  return leftChild != null;
}

I know all about Java but that doesn't help me do this in C++. When I compile in Visual C++ 6.0 I get error "C2676: binary '==' : 'class Node' does not define this operator or a conversion..."

I can understand why it can't compare two 'Node' objects, because I didn't overload that operator. However, I don't want to do a deep comparison, just a shallow comparison. This works fine in Java, but I don't know the proper syntax in C++.

I forgot to say that the variable "leftChild" is declared in the class header as being of class "Node", the superclass of this one.
I think that if you make leftChild a pointer to an object of the class Node, you can set the pointer to null with something like:

      leftChild = (Node*)0;

and check for null like:

      if (leftChild == (Node*)0) {
            ...


Hope that helps.
Good idea, but it didn't work. I get basically the same error now, even after converting all variables to pointers.

I think what I will do is make a subclass or variant of the "Leaf" class and use it for non-existant leaf nodes. Since the tree is populated and then never changes, I don't care how efficient it is to fill, even if it means more variables or classes. Just so it is efficient to traverse the tree. I'll probably just add a "isNull" variable to the Leaf class (which is a subclass of the "Node" class).
Avatar of ozo
What is the declaraton of leftChild?
class BSPTree : public Node {
private:
Node leftChild;
// omitting everything else...
};

I don't have it initialized anywhere just yet, I am still writing the class.

As far as I am aware, a NULL reference is invalid.  You may have to change the leftChild member variable to be a pointer to a node rather than a variable of type node.   Then the pointer can be set to NULL.

class BSPTree : public Node {
private:
  Node *leftChild;  
 // omitting everything else...
};

bool BSPTree::hasLeftChild()
{
  return leftChild != NULL;
}

Not sure if the use of pointers is what you wree looking for though.  (Can you tell I learned C before C++??  :)  )

Note: that in a desctructor, you would need to ensure you recurse through all leftCHild members to delete them all.

Woodster
If that approach is what you are after, let me know if you need more info.
I still get an error stating that "NULL" is an undeclared identifier.

In the destructor, I will probably just use recursion to delete all left *and* right child nodes. Remember, each node has a potential of two children. . . this is a binary tree, after all. I know there are other ways of doing this, I just need an extremely efficient method. each line of code I add to this function will add over one million instructions per second, which is unacceptable even on fast processors. Speed is the first priority, even over size. I don't care if this tree needs several megabytes, just so it is fast. I probably will use pointers and scrap the accessor/mutator function in favor of direct access of data members.

I will lose some stability this way, so I'll just have to be careful when I write the functions that retrieve data from the tree.
I think NULL is defined in one of the standard include files (maybe stdlib.h).  If it isn't, I think that just setting a pointer to 0 will work...  You can define it yourself with "#define NULL 0" at the beginning of your program (without the quotes).  You may also need to cast NULL to the correct type when using it.
ASKER CERTIFIED SOLUTION
Avatar of mdlilly
mdlilly

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent! A definitive answer that leaves no room for doubt. I know Java is a lot like C++ under the covers, especially with regard to pointers. I am used to just setting a reference variable to "null", not making it a pointer and doing it that way. Since I need to optimize for speed, I am making this class a complete glass-box and removing all the accessors. I will take your advice and add a flag for "nullness" to the Leaf class (since the Node class is only used as a superclass for polymorphism, no objects are ever created).