Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Proper Use of NULL

Reply
Thread Tools

Proper Use of NULL

 
 
cppaddict
Guest
Posts: n/a
 
      05-20-2004
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
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      05-20-2004

"cppaddict" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
bartek
Guest
Posts: n/a
 
      05-20-2004
cppaddict <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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.
 
Reply With Quote
 
Alan Johnson
Guest
Posts: n/a
 
      05-20-2004
Howard wrote:

> "cppaddict" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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
>
>
>


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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-20-2004
>
> 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


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      05-20-2004

"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >
> > 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
>
>


D'OH!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why use "return (null);" instead of "return null;" ? Carl Java 21 08-24-2006 04:33 AM
When to use null and when to use static_cast<some_pointer_type>(0)? PengYu.UT@gmail.com C++ 19 04-04-2006 04:58 PM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
VB.NET Null to SQL Null (ASP.NET 2.0 GridView) Kivak Wolf ASP .Net 2 06-28-2005 02:01 PM
Proper way to use an imported constant under 'use strict'? H. Wade Minter Perl Misc 8 04-25-2004 12:58 AM



Advertisments