Problem with checking if class is NULL

  • Thread starter TwinkiE_HunteR-G
  • Start date
T

TwinkiE_HunteR-G

Hello all,

Ok, I am taking a C++ class at a local community college, and I'm
working on a small program for my own learning needs and I ran into a
problem.

The class I'm building includes a pointer to another object of the
same class, so that I can loop through the objects, using the objects
Next_Object() member function which returns the adress of the next
object in the list.

In my loop I am setting a pointer to the class to what Next_Object()
returns, and I need to check if that is NULL in order to exit the loop
(i.e tmp_object != NULL). Here is where my problem is. The compiler
says that I need to overload the != operator to do this, but if the
object is NULL, how would it call it's member function?

Any help would be appreciated.


- TwinkiE_HunteR-G strikes again!
: (e-mail address removed)
: HARD-WiRE Web Design
: http://www.hard-wire.net
 
J

Jack Klein

Hello all,

Ok, I am taking a C++ class at a local community college, and I'm
working on a small program for my own learning needs and I ran into a
problem.

The class I'm building includes a pointer to another object of the
same class, so that I can loop through the objects, using the objects
Next_Object() member function which returns the adress of the next
object in the list.

In my loop I am setting a pointer to the class to what Next_Object()
returns, and I need to check if that is NULL in order to exit the loop
(i.e tmp_object != NULL). Here is where my problem is. The compiler
says that I need to overload the != operator to do this, but if the
object is NULL, how would it call it's member function?

Any help would be appreciated.

There must be a difference between the description that you posted and
the code that you didn't. Any pointer to any object type, and even
pointers to functions can be compared to NULL. If your compiler does
not accept this, what you are trying to compare is NOT a pointer.

Post the smallest possible code sample that show problem, with the
definitions of the data types.
 
J

Jon Willeke

TwinkiE_HunteR-G said:
In my loop I am setting a pointer to the class to what Next_Object()
returns, and I need to check if that is NULL in order to exit the loop
(i.e tmp_object != NULL). Here is where my problem is. The compiler
says that I need to overload the != operator to do this, but if the
object is NULL, how would it call it's member function?

Any help would be appreciated.

Are you sure tmp_object is a pointer?
 
G

Gernot Frisch

Jon Willeke said:
Are you sure tmp_object is a pointer?

And if it's not:
class A
{
public:
A(){}
~A(){}

A m_A;
};

int main(int, char**)
{
A myA;
}

This will get you some trouble, won't it? My compiler doesn't allow
it, does yours?
-Gernot
 
T

TwinkiE_HunteR-G

And if it's not:
class A
{
public:
A(){}
~A(){}

A m_A;
};

int main(int, char**)
{
A myA;
}

This will get you some trouble, won't it? My compiler doesn't allow
it, does yours?
-Gernot

OK, Im sorry for the trouble, I have been up for a couple of days when
I wrote this and I missed the single character to declare tmp_object
as a pointer. My apologizes and thanks for the quick and helpful
responses.


- TwinkiE_HunteR-G strikes again!
: (e-mail address removed)
: HARD-WiRE Web Design
: http://www.hard-wire.net
 
B

Buster

Anil said:
I think NULL is defined in stdio.h

I think you're right, but stdio.h is deprecated. NULL is defined in the
standard headers clocale, cstddef, cstdio, cstdlib, cstring, ctime and
cwchar. I posted this information earlier on without attribution. I got
it from the marvellous "C++ in a Nutshell" by Ray Lischner.
 
B

Bill Seurer

Christopher said:
NULL is equally proper.

The only mention of "NULL" in the standard is in the description of
cstddef. Also see section 5.1.1 of _The C++ Programming Language_.
 
K

Kevin Goodsell

Bill said:
The only mention of "NULL" in the standard is in the description of
cstddef. Also see section 5.1.1 of _The C++ Programming Language_.

I see many mentions of NULL scattered throughout the Nov. '97 draft that
I have. In particular, C.2.2.3 (non-normative) lists <clocale>,
<cstddef>, <cstdio>, <cstdlib>, <cstring>, <ctime>, and <cwchar> as
containing a definition for NULL.

It probably defers to the C standard to formally give the headers where
NULL is defined.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top