Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Null

Posted on 1999-06-28
13
Medium Priority
?
448 Views
Last Modified: 2010-04-02
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").
0
Comment
Question by:TheSnowman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +3
13 Comments
 
LVL 6

Expert Comment

by:Triskelion
ID: 1198633
Yes.
Use all caps

char * GetNull(void)
{
   static char * spData=NULL;
   return spData;
}
0
 

Author Comment

by:TheSnowman
ID: 1198634
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++.

0
 

Author Comment

by:TheSnowman
ID: 1198635
I forgot to say that the variable "leftChild" is declared in the class header as being of class "Node", the superclass of this one.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:mattrope
ID: 1198636
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.
0
 

Author Comment

by:TheSnowman
ID: 1198637
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).
0
 
LVL 84

Expert Comment

by:ozo
ID: 1198638
What is the declaraton of leftChild?
0
 

Author Comment

by:TheSnowman
ID: 1198639
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.

0
 
LVL 4

Expert Comment

by:Woodster
ID: 1198640
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
0
 
LVL 4

Expert Comment

by:Woodster
ID: 1198641
If that approach is what you are after, let me know if you need more info.
0
 

Author Comment

by:TheSnowman
ID: 1198642
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.
0
 
LVL 2

Expert Comment

by:mattrope
ID: 1198643
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.
0
 
LVL 1

Accepted Solution

by:
mdlilly earned 200 total points
ID: 1198644
The answer is that you can not have an intance of a data type be NULL.  Even in java what you deal with are not instances of an object but rather a reference (pointer) to an instance of an object.  So when in Java you do this:

MyClass x;
x = null;
return x;

it is the EXACT same (down dirty where it counts) as saying
MyClass *x;
x = NULL;
return x;

it's just that Java hides the pointer'y ness from us so we get the power of pointers (almost) at a greatly reduced price.

The only thing you could do is in your node class make a "reference flag" that you set when you decide you want a node to become "non-null".

mdl
0
 

Author Comment

by:TheSnowman
ID: 1198645
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).
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question