Proper Use of NULL

C

cppaddict

Hi,

A quick question regarding the use of NULL.

I have a Reader class, which reads from a Display. Reader has two
relevant members for this question:

CharsetCollection
-- a collection of possible Charsets being used by Display
activeCharset
-- the actual Charset being used by Display

The first time a Reader object invokes its findNextChar() method, it
will search every character in each of its Charsets and try to find a
match for the character at the current cursor position in Display. It
will then set activeCharset to the appropriate Charset based on the
search results, so that on future searches only that Charset will be
searched.

Right now, I have the constructor of Reader setting
activeCharset(NULL) in its initialization list.

Then my findNextChar() method looks like this:

char Reader::findNextChar() {
if (activeCharset == NULL) { //compiler errors here
search all Charsets
set activeCharset based on search results
} else {
search only activeCharset
}
}

My question is: What is the right way to do what I am trying to do.
Am I simply using NULL incorrectly? Is my design on the wrong track
altogether?

thanks for any ideas,
cpp
 
H

Howard

cppaddict said:
Hi,

char Reader::findNextChar() {
if (activeCharset == NULL) { //compiler errors here
search all Charsets
set activeCharset based on search results
} else {
search only activeCharset
}
}

My question is: What is the right way to do what I am trying to do.
Am I simply using NULL incorrectly? Is my design on the wrong track
altogether?

You don't say what the specific error is, or give us the declaration of
activeCharset, so we can't say for sure. But, if activeCharset is a
pointer, you can use nil. If it's an integer, you can use 0. (The term
NULL, I think, is not part of the standard, and is often defined as 0.)
-Howard
 
B

bartek

My question is: What is the right way to do what I am trying to do.
Am I simply using NULL incorrectly? Is my design on the wrong track
altogether?

NULL is zero (0) in C++. Should be used with pointers only.
Is activeCharset a pointer?

All in all, it's better to use a plain zero (0) instead of NULL. Less
confusion, etc.

Besides, NULL is a preprocessord definition, and as we all know,
preprocessor is evil. period.

On the other hand... Are the guys behind Boost.Preprocessor from hell? huh?
If the preprocessor alone is evil, then that library is the purest, most
condensed, archetypal evil to walk the earth, soon reaching for critical
mass! :)

But I digress...

Cheers.
 
A

Alan Johnson

Howard said:
You don't say what the specific error is, or give us the declaration of
activeCharset, so we can't say for sure. But, if activeCharset is a
pointer, you can use nil. If it's an integer, you can use 0. (The term
NULL, I think, is not part of the standard, and is often defined as 0.)
-Howard

The standard says that NULL exists, but is implementation-defined.

Section 18.1, line 4 :

"The macro NULL is an implementation-defined C + + null pointer constant
in this International Standard (4.10)."

It also mentions that it is defined in <cstddef>.

Section 4.10 starts by defining a "null pointer constant" :

"A null pointer constant is an integral constant expression (5.19)
rvalue of integer type that evaluates to zero."


Alan


Alan
 
J

John Harrison

You don't say what the specific error is, or give us the declaration of
activeCharset, so we can't say for sure. But, if activeCharset is a
pointer, you can use nil. If it's an integer, you can use 0. (The term
NULL, I think, is not part of the standard, and is often defined as 0.)
-Howard

Wrong way round, NULL is standard, nil is not.

john
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top