How to initialize a pointer in c++,

O

Old Wolf

marius said:
Yes, but these are the "old" C header files wrapped in the std
namespace. C++ has no need for NULL.

They are standard C++ headers. NULL is a part of C++.
Whether there is a "need" for NULL is a matter of opinion.

You could say that C does not need NULL either, you can
type (void *)0 everywhere.
 
P

Phlip

marius said:
Yes, but these are the "old" C header files wrapped in the std
namespace. C++ has no need for NULL.

All code should clearly state its intent to the reader. if(p) may be fun and
cute, but if(NULL == p) clearly states its intent.
 
R

Richard Herring

Phlip said:
All code should clearly state its intent to the reader. if(p) may be fun and
cute, but if(NULL == p) clearly states its intent.
But, depending on what your p actually represents, the ownership model,
etc. etc., one of the following
if (is_a_valid_pointer(p))
if (!has_expired(p))
if (is_allocated(p))
would be clearer still, and also avoids the problem that caused you to
write your comparison the unnatural way round. Where do you draw the
line?

Personally I'd consider if (p) to be a natural idiom of the language,
and therefore perfectly clear and, moreover, concise.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top